Skip to content

Commit

Permalink
Merge #2374: [Backport] Fix memory leaks in qt/guiutil.cpp
Browse files Browse the repository at this point in the history
aad1362 scripted-diff: Replace 'NULL' with 'nullptr' in guiutil.cpp (random-zebra)
5c07620 Fix memory leaks in qt/guiutil.cpp (Dan Raviv)

Pull request description:

  From bitcoin#11156

  > on macOS:
  > listSnapshot was leaking in findStartupItemInList()
  > bitcoinAppUrl was leaking in [Get|Set]StartOnSystemStartup()

ACKs for top commit:
  Fuzzbawls:
    utACK aad1362
  furszy:
    utACK aad1362 and merging..

Tree-SHA512: 640714832bed4513d14875294175085b30d1473dc3497e247959863867f743abe926cf6ba18bb42982e81906bf741b1c86c0953bad742f6e0772467ae7d6f0c1
  • Loading branch information
furszy committed May 11, 2021
2 parents 10319fd + aad1362 commit 4a6d100
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions src/qt/guiutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ bool SetStartOnSystemStartup(bool fAutoStart)
fs::remove(StartupShortcutPath());

if (fAutoStart) {
CoInitialize(NULL);
CoInitialize(nullptr);

// Get a pointer to the IShellLink interface.
IShellLinkW* psl = nullptr;
Expand Down Expand Up @@ -773,57 +773,76 @@ bool SetStartOnSystemStartup(bool fAutoStart)
LSSharedFileListItemRef findStartupItemInList(LSSharedFileListRef list, CFURLRef findUrl);
LSSharedFileListItemRef findStartupItemInList(LSSharedFileListRef list, CFURLRef findUrl)
{
CFArrayRef listSnapshot = LSSharedFileListCopySnapshot(list, nullptr);
if (listSnapshot == nullptr) {
return nullptr;
}

// loop through the list of startup items and try to find the pivx app
CFArrayRef listSnapshot = LSSharedFileListCopySnapshot(list, NULL);
for (int i = 0; i < CFArrayGetCount(listSnapshot); i++) {
LSSharedFileListItemRef item = (LSSharedFileListItemRef)CFArrayGetValueAtIndex(listSnapshot, i);
UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
CFURLRef currentItemURL = NULL;
CFURLRef currentItemURL = nullptr;

#if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && MAC_OS_X_VERSION_MAX_ALLOWED >= 10100
if (&LSSharedFileListItemCopyResolvedURL)
currentItemURL = LSSharedFileListItemCopyResolvedURL(item, resolutionFlags, NULL);
currentItemURL = LSSharedFileListItemCopyResolvedURL(item, resolutionFlags, nullptr);
#if defined(MAC_OS_X_VERSION_MIN_REQUIRED) && MAC_OS_X_VERSION_MIN_REQUIRED < 10100
else
LSSharedFileListItemResolve(item, resolutionFlags, &currentItemURL, NULL);
LSSharedFileListItemResolve(item, resolutionFlags, &currentItemURL, nullptr);
#endif
#else
LSSharedFileListItemResolve(item, resolutionFlags, &currentItemURL, NULL);
LSSharedFileListItemResolve(item, resolutionFlags, &currentItemURL, nullptr);
#endif

if (currentItemURL && CFEqual(currentItemURL, findUrl)) {
// found
CFRelease(currentItemURL);
return item;
}
if (currentItemURL) {
if (CFEqual(currentItemURL, findUrl)) {
// found
CFRelease(listSnapshot);
CFRelease(currentItemURL);
return item;
}
CFRelease(currentItemURL);
}
}
return NULL;

CFRelease(listSnapshot);
return nullptr;
}

bool GetStartOnSystemStartup()
{
CFURLRef bitcoinAppUrl = CFBundleCopyBundleURL(CFBundleGetMainBundle());
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (bitcoinAppUrl == nullptr) {
return false;
}

LSSharedFileListRef loginItems = LSSharedFileListCreate(nullptr, kLSSharedFileListSessionLoginItems, nullptr);
LSSharedFileListItemRef foundItem = findStartupItemInList(loginItems, bitcoinAppUrl);

CFRelease(bitcoinAppUrl);
return !!foundItem; // return boolified object
}

bool SetStartOnSystemStartup(bool fAutoStart)
{
CFURLRef bitcoinAppUrl = CFBundleCopyBundleURL(CFBundleGetMainBundle());
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (bitcoinAppUrl == nullptr) {
return false;
}

LSSharedFileListRef loginItems = LSSharedFileListCreate(nullptr, kLSSharedFileListSessionLoginItems, nullptr);
LSSharedFileListItemRef foundItem = findStartupItemInList(loginItems, bitcoinAppUrl);

if (fAutoStart && !foundItem) {
// add pivx app to startup item list
LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemBeforeFirst, NULL, NULL, bitcoinAppUrl, NULL, NULL);
LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemBeforeFirst, nullptr, nullptr, bitcoinAppUrl, nullptr, nullptr);
} else if (!fAutoStart && foundItem) {
// remove item
LSSharedFileListItemRemove(loginItems, foundItem);
}

CFRelease(bitcoinAppUrl);
return true;
}
#pragma GCC diagnostic pop
Expand Down

0 comments on commit 4a6d100

Please sign in to comment.