Delete QWindow children during QWindow destruction before ~QObject

Otherwise we run the risk of a child window referencing its parent
as a QWindow, for example via QWindow::screen(), which is UB, as the
QWindow no longer exists as such.

It's the right fix, but it may have fallout from code that (wrongly)
assumed children would live longer than what they will after this patch.
The reason for not picking to 6.9 or earlier is to mitigate this risk.
We can always pick later once it's cooked in dev.

Done-with: Marc Mutz <marc.mutz@qt.io>
Change-Id: Idca3fe09b83c7bcd9335a48877f2f14b304fb0ac
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Tor Arne Vestbø 2025-03-27 11:39:21 +01:00 committed by Marc Mutz
parent 8edabea2a7
commit 92a65fdac6

View File

@ -184,6 +184,12 @@ QWindow::QWindow(QWindowPrivate &dd, QWindow *parent)
QWindow::~QWindow()
{
Q_D(QWindow);
// Delete child windows up front, instead of waiting for ~QObject,
// in case the destruction of the child references its parent as
// a (no longer valid) QWindow.
qDeleteAll(findChildren<QWindow *>(Qt::FindDirectChildrenOnly));
d->destroy();
// Decouple from parent before window goes under
setParent(nullptr);