From 0367760fedd2edd708ba30c73cd881073debbecb Mon Sep 17 00:00:00 2001 From: Evan Mezeske Date: Fri, 29 Mar 2024 13:50:30 -0700 Subject: [PATCH] Fix crash after MessageManager deleted/recreated If the MessageManager is deleted (via e.g. deleteInstance()) and then re-created in the same process, the next time that a top-level window is created, the process will crash because the JuceIVirtualDesktopManager COM object pointer is cached in a static variable, and the MessageManager deconstructor calls OleUninitialize() which shuts down COM, thereby invalidating the static cached pointer. There's no reason to cache this anyway, as CoCreateInstance() returns a singleton, and this is not performance-sensitive code in a tight loop or something. I found this bug because in my unit testing, I create and destroy the message manager for each test to ensure that the tests are hermetic. With this fix in place it works perfectly. --- .../native/juce_Windowing_windows.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/modules/juce_gui_basics/native/juce_Windowing_windows.cpp b/modules/juce_gui_basics/native/juce_Windowing_windows.cpp index f898fa3c2060..89b44e74add8 100644 --- a/modules/juce_gui_basics/native/juce_Windowing_windows.cpp +++ b/modules/juce_gui_basics/native/juce_Windowing_windows.cpp @@ -5336,19 +5336,14 @@ bool juce::detail::WindowingHelpers::isWindowOnCurrentVirtualDesktop (void* x) if (x == nullptr) return false; - static auto* desktopManager = [] - { - JuceIVirtualDesktopManager* result = nullptr; - - JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wlanguage-extension-token") + JuceIVirtualDesktopManager* desktopManager = nullptr; - if (SUCCEEDED (CoCreateInstance (__uuidof (JuceVirtualDesktopManager), nullptr, CLSCTX_ALL, IID_PPV_ARGS (&result)))) - return result; + JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wlanguage-extension-token") - JUCE_END_IGNORE_WARNINGS_GCC_LIKE + if (FAILED (CoCreateInstance (__uuidof (JuceVirtualDesktopManager), nullptr, CLSCTX_ALL, IID_PPV_ARGS (&desktopManager)))) + return true; - return static_cast (nullptr); - }(); + JUCE_END_IGNORE_WARNINGS_GCC_LIKE BOOL current = false;