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 <timur.pocheptsov@qt.io>
(cherry picked from commit abb985a4032a337581aa885e1ac547287244b695)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tor Arne Vestbø 2020-08-06 16:16:49 +02:00 committed by Qt Cherry-pick Bot
parent 25e0e6273d
commit caa68b9c53

View File

@ -357,6 +357,14 @@ QCALayerBackingStore::QCALayerBackingStore(QWindow *window)
NSView *view = static_cast<QCocoaWindow *>(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();