From 668768ade5c7fb119c455417965628a4e50feac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Tue, 22 Jun 2021 11:43:18 +0200 Subject: macOS: Unregister screen notification handlers on QGuiApplication shutdown In the case of a plugin, the plugin might be unloaded, and destroy its QGuiApplication. We don't want the screen notification handlers to outlive the application, as that results in crashes. Fixes: QTBUG-91919 Done-with: Yang Yang Change-Id: I3a4c0fcf97b785357516d1dac34489511400f154 Reviewed-by: Alexandru Croitor (cherry picked from commit 96469ae338c3146f4efea3fabaaae5bc6ecd6a59) Reviewed-by: Qt Cherry-pick Bot --- src/plugins/platforms/cocoa/qcocoascreen.h | 4 ++++ src/plugins/platforms/cocoa/qcocoascreen.mm | 15 ++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/plugins/platforms/cocoa/qcocoascreen.h b/src/plugins/platforms/cocoa/qcocoascreen.h index b109c24f63..0eb6d114e2 100644 --- a/src/plugins/platforms/cocoa/qcocoascreen.h +++ b/src/plugins/platforms/cocoa/qcocoascreen.h @@ -43,6 +43,7 @@ #include "qcocoacursor.h" #include +#include #include #include @@ -102,6 +103,9 @@ private: static void updateScreens(); static void cleanupScreens(); + static QMacNotificationObserver s_screenParameterObserver; + static CGDisplayReconfigurationCallBack s_displayReconfigurationCallBack; + static bool updateScreensIfNeeded(); static NSArray *s_screenConfigurationBeforeUpdate; diff --git a/src/plugins/platforms/cocoa/qcocoascreen.mm b/src/plugins/platforms/cocoa/qcocoascreen.mm index 61f7476159..d326d149f1 100644 --- a/src/plugins/platforms/cocoa/qcocoascreen.mm +++ b/src/plugins/platforms/cocoa/qcocoascreen.mm @@ -75,12 +75,14 @@ namespace CoreGraphics { } NSArray *QCocoaScreen::s_screenConfigurationBeforeUpdate = nil; +QMacNotificationObserver QCocoaScreen::s_screenParameterObserver; +CGDisplayReconfigurationCallBack QCocoaScreen::s_displayReconfigurationCallBack = nullptr; void QCocoaScreen::initializeScreens() { updateScreens(); - CGDisplayRegisterReconfigurationCallback([](CGDirectDisplayID displayId, CGDisplayChangeSummaryFlags flags, void *userInfo) { + s_displayReconfigurationCallBack = [](CGDirectDisplayID displayId, CGDisplayChangeSummaryFlags flags, void *userInfo) { Q_UNUSED(userInfo); // Displays are reconfigured in batches, and we want to update our screens @@ -131,9 +133,10 @@ void QCocoaScreen::initializeScreens() updateScreensIfNeeded(); } } - }, nullptr); + }; + CGDisplayRegisterReconfigurationCallback(s_displayReconfigurationCallBack, nullptr); - static QMacNotificationObserver screenParameterObserver(NSApplication.sharedApplication, + s_screenParameterObserver = QMacNotificationObserver(NSApplication.sharedApplication, NSApplicationDidChangeScreenParametersNotification, [&]() { qCDebug(lcQpaScreen) << "Received screen parameter change notification"; updateScreensIfNeeded(); // As a last resort we update screens here @@ -241,6 +244,12 @@ void QCocoaScreen::cleanupScreens() // Remove screens in reverse order to avoid crash in case of multiple screens for (QScreen *screen : backwards(QGuiApplication::screens())) static_cast(screen->handle())->remove(); + + Q_ASSERT(s_displayReconfigurationCallBack); + CGDisplayRemoveReconfigurationCallback(s_displayReconfigurationCallBack, nullptr); + s_displayReconfigurationCallBack = nullptr; + + s_screenParameterObserver.remove(); } void QCocoaScreen::remove() -- cgit v1.2.3