From caa68b9c538aa30ec6fc3feed2847fbe53d904b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Thu, 6 Aug 2020 16:16:49 +0200 Subject: [PATCH] macOS: Fix crash when re-using backingstore for re-created QWindow The observer we add for the NSWindow of the backingstore window will not be valid once that window is destroyed, but we may get last minute notifications for it still, in which case our platform window is gone. This patch fixes the immediate crash, but reveals a more fundamental problem of assuming the platform window that's alive during construction is the one that will always be used with the backingstore. This is not the case when for example QtWidgets opens a combo box, and reuses the backingstore for the widget each time the combobox popup is shown. Fixes: QTBUG-85915 Change-Id: Ia66a45d003882602ac29476aabf2d58b0ac33c1e Reviewed-by: Timur Pocheptsov (cherry picked from commit abb985a4032a337581aa885e1ac547287244b695) Reviewed-by: Qt Cherry-pick Bot --- src/plugins/platforms/cocoa/qcocoabackingstore.mm | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/plugins/platforms/cocoa/qcocoabackingstore.mm b/src/plugins/platforms/cocoa/qcocoabackingstore.mm index 6aa0c0182f2..6ea6714649f 100644 --- a/src/plugins/platforms/cocoa/qcocoabackingstore.mm +++ b/src/plugins/platforms/cocoa/qcocoabackingstore.mm @@ -357,6 +357,14 @@ QCALayerBackingStore::QCALayerBackingStore(QWindow *window) NSView *view = static_cast(window->handle())->view(); m_backingPropertiesObserver = QMacNotificationObserver(view.window, NSWindowDidChangeBackingPropertiesNotification, [this]() { + if (!this->window()->handle()) { + // The platform window has been destroyed, but the backingstore + // is still alive, as that's tied to a QWindow. The original + // NSWindow we were observing is also likely gone. FIXME: + // We should listen for surface events from the QWindow and + // remove and re-attach our observer based on those. + return; + } qCDebug(lcQpaBackingStore) << "Backing properties for" << this->window() << "did change"; backingPropertiesChanged();