From 112f8999fdb731445a096155ce590469a9db9b6f Mon Sep 17 00:00:00 2001 From: ed Date: Tue, 7 Dec 2021 14:34:57 +0000 Subject: [PATCH] iOS: Fix native message box leak when no callback is provided --- .../native/juce_ios_Windowing.mm | 21 +++++++++++++------ .../native/juce_mac_Windowing.mm | 2 +- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/modules/juce_gui_basics/native/juce_ios_Windowing.mm b/modules/juce_gui_basics/native/juce_ios_Windowing.mm index 21f8411f11..bedbfc4319 100644 --- a/modules/juce_gui_basics/native/juce_ios_Windowing.mm +++ b/modules/juce_gui_basics/native/juce_ios_Windowing.mm @@ -437,8 +437,11 @@ void LookAndFeel::playAlertSound() class iOSMessageBox { public: - iOSMessageBox (const MessageBoxOptions& opts, std::unique_ptr&& cb) - : callback (std::move (cb)) + iOSMessageBox (const MessageBoxOptions& opts, + std::unique_ptr&& cb, + bool deleteOnCompletion) + : callback (std::move (cb)), + shouldDeleteThis (deleteOnCompletion) { if (currentlyFocusedPeer != nullptr) { @@ -480,10 +483,10 @@ public: result = buttonIndex; if (callback != nullptr) - { callback->modalStateFinished (result); + + if (shouldDeleteThis) delete this; - } } private: @@ -501,6 +504,7 @@ private: int result = -1; std::unique_ptr callback; + const bool shouldDeleteThis; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (iOSMessageBox) }; @@ -517,13 +521,18 @@ static int showDialog (const MessageBoxOptions& options, { jassert (mapFn != nullptr); - iOSMessageBox messageBox (options, nullptr); + iOSMessageBox messageBox (options, nullptr, false); return mapFn (messageBox.getResult()); } } #endif - const auto showBox = [options, callbackIn, mapFn] { new iOSMessageBox (options, AlertWindowMappings::getWrappedCallback (callbackIn, mapFn)); }; + const auto showBox = [options, callbackIn, mapFn] + { + new iOSMessageBox (options, + AlertWindowMappings::getWrappedCallback (callbackIn, mapFn), + true); + }; if (MessageManager::getInstance()->isThisTheMessageThread()) showBox(); diff --git a/modules/juce_gui_basics/native/juce_mac_Windowing.mm b/modules/juce_gui_basics/native/juce_mac_Windowing.mm index 65dd9d88f4..2f9778cbb6 100644 --- a/modules/juce_gui_basics/native/juce_mac_Windowing.mm +++ b/modules/juce_gui_basics/native/juce_mac_Windowing.mm @@ -82,7 +82,7 @@ private: [alert setInformativeText: juceStringToNS (options.getMessage())]; [alert setAlertStyle: options.getIconType() == MessageBoxIconType::WarningIcon ? NSAlertStyleCritical - : NSAlertStyleInformational]; + : NSAlertStyleInformational]; const auto button1Text = options.getButtonText (0);