Skip to content

Commit a3624dd

Browse files
committed
Merge #11156: Fix memory leaks in qt/guiutil.cpp
9b348ff Fix memory leaks in qt/guiutil.cpp (Dan Raviv) Pull request description: on macOS: `listSnapshot` was leaking in `findStartupItemInList()` `bitcoinAppUrl` was leaking in `[Get|Set]StartOnSystemStartup()` Tree-SHA512: dd49e1166336cf4f20035d21930f2f99f21f1d9f91a1101b1434a23dd0b92d402ac7efb177473c758d8af1dbab8d8750485583231c5b5854203d2493f0b43e73
2 parents ea729d5 + 9b348ff commit a3624dd

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

src/qt/guiutil.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -781,47 +781,64 @@ bool SetStartOnSystemStartup(bool fAutoStart)
781781
LSSharedFileListItemRef findStartupItemInList(LSSharedFileListRef list, CFURLRef findUrl);
782782
LSSharedFileListItemRef findStartupItemInList(LSSharedFileListRef list, CFURLRef findUrl)
783783
{
784-
// loop through the list of startup items and try to find the bitcoin app
785784
CFArrayRef listSnapshot = LSSharedFileListCopySnapshot(list, nullptr);
785+
if (listSnapshot == nullptr) {
786+
return nullptr;
787+
}
788+
789+
// loop through the list of startup items and try to find the bitcoin app
786790
for(int i = 0; i < CFArrayGetCount(listSnapshot); i++) {
787791
LSSharedFileListItemRef item = (LSSharedFileListItemRef)CFArrayGetValueAtIndex(listSnapshot, i);
788792
UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
789793
CFURLRef currentItemURL = nullptr;
790794

791795
#if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && MAC_OS_X_VERSION_MAX_ALLOWED >= 10100
792-
if(&LSSharedFileListItemCopyResolvedURL)
793-
currentItemURL = LSSharedFileListItemCopyResolvedURL(item, resolutionFlags, nullptr);
796+
if(&LSSharedFileListItemCopyResolvedURL)
797+
currentItemURL = LSSharedFileListItemCopyResolvedURL(item, resolutionFlags, nullptr);
794798
#if defined(MAC_OS_X_VERSION_MIN_REQUIRED) && MAC_OS_X_VERSION_MIN_REQUIRED < 10100
795-
else
796-
LSSharedFileListItemResolve(item, resolutionFlags, &currentItemURL, nullptr);
799+
else
800+
LSSharedFileListItemResolve(item, resolutionFlags, &currentItemURL, nullptr);
797801
#endif
798802
#else
799-
LSSharedFileListItemResolve(item, resolutionFlags, &currentItemURL, nullptr);
803+
LSSharedFileListItemResolve(item, resolutionFlags, &currentItemURL, nullptr);
800804
#endif
801805

802-
if(currentItemURL && CFEqual(currentItemURL, findUrl)) {
803-
// found
804-
CFRelease(currentItemURL);
805-
return item;
806-
}
807806
if(currentItemURL) {
807+
if (CFEqual(currentItemURL, findUrl)) {
808+
// found
809+
CFRelease(listSnapshot);
810+
CFRelease(currentItemURL);
811+
return item;
812+
}
808813
CFRelease(currentItemURL);
809814
}
810815
}
816+
817+
CFRelease(listSnapshot);
811818
return nullptr;
812819
}
813820

814821
bool GetStartOnSystemStartup()
815822
{
816823
CFURLRef bitcoinAppUrl = CFBundleCopyBundleURL(CFBundleGetMainBundle());
824+
if (bitcoinAppUrl == nullptr) {
825+
return false;
826+
}
827+
817828
LSSharedFileListRef loginItems = LSSharedFileListCreate(nullptr, kLSSharedFileListSessionLoginItems, nullptr);
818829
LSSharedFileListItemRef foundItem = findStartupItemInList(loginItems, bitcoinAppUrl);
830+
831+
CFRelease(bitcoinAppUrl);
819832
return !!foundItem; // return boolified object
820833
}
821834

822835
bool SetStartOnSystemStartup(bool fAutoStart)
823836
{
824837
CFURLRef bitcoinAppUrl = CFBundleCopyBundleURL(CFBundleGetMainBundle());
838+
if (bitcoinAppUrl == nullptr) {
839+
return false;
840+
}
841+
825842
LSSharedFileListRef loginItems = LSSharedFileListCreate(nullptr, kLSSharedFileListSessionLoginItems, nullptr);
826843
LSSharedFileListItemRef foundItem = findStartupItemInList(loginItems, bitcoinAppUrl);
827844

@@ -833,6 +850,8 @@ bool SetStartOnSystemStartup(bool fAutoStart)
833850
// remove item
834851
LSSharedFileListItemRemove(loginItems, foundItem);
835852
}
853+
854+
CFRelease(bitcoinAppUrl);
836855
return true;
837856
}
838857
#pragma GCC diagnostic pop

0 commit comments

Comments
 (0)