From 697643c0b6752b137a589e7e1c1bafe5d1c714d7 Mon Sep 17 00:00:00 2001 From: reuk Date: Tue, 25 Oct 2022 13:30:34 +0100 Subject: [PATCH] Component: Add an assertion to check that the component remains alive when entering modal state --- modules/juce_gui_basics/components/juce_Component.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/modules/juce_gui_basics/components/juce_Component.cpp b/modules/juce_gui_basics/components/juce_Component.cpp index cc7577c5db..7eb1d58918 100644 --- a/modules/juce_gui_basics/components/juce_Component.cpp +++ b/modules/juce_gui_basics/components/juce_Component.cpp @@ -1769,6 +1769,8 @@ void Component::enterModalState (bool shouldTakeKeyboardFocus, // thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED + SafePointer safeReference { this }; + if (! isCurrentlyModal (false)) { // While this component is in modal state it may block other components from receiving @@ -1776,6 +1778,13 @@ void Component::enterModalState (bool shouldTakeKeyboardFocus, // we must manually force the mouse to "leave" blocked components. ComponentHelpers::sendMouseEventToComponentsThatAreBlockedByModal (*this, &Component::internalMouseExit); + if (safeReference == nullptr) + { + // If you hit this assertion, the mouse-exit event above has caused the modal component to be deleted. + jassertfalse; + return; + } + auto& mcm = *ModalComponentManager::getInstance(); mcm.startModal (this, deleteWhenDismissed); mcm.attachCallback (this, callback);