From 3bcc785d0cd8bd3e41ef1f3ebf3172815e68f1b3 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Mon, 20 Jan 2025 10:20:48 +0100 Subject: [PATCH] Avoid dangling d-pointer deref in QWidgetWindow::handleMouseEvent If you drag-and-drop a OpenGLWidget in Designer, the main window gets re-created when the OpenGLWidget is instantiated. So in general (in rare cases), at the end of QWidgetWindow::handleMouseEvent() we might have a different window, and therefore can't reliably call QWindowPrivate::maybeSynthesizeContextMenuEvent() without checking for a valid pointer. Amends 84a5f50c7766c99f62b22bb4388137e0aa8dd13d Fixes: QTBUG-132912 Change-Id: I7b220b4daceab988aadabf9427ef6b2d5624e00d Reviewed-by: Friedemann Kleint (cherry picked from commit 1accd2421686b387daa115b3a46974fce0fdb118) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/kernel/qwidgetwindow.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/widgets/kernel/qwidgetwindow.cpp b/src/widgets/kernel/qwidgetwindow.cpp index 0565835e38b..53bf13a5735 100644 --- a/src/widgets/kernel/qwidgetwindow.cpp +++ b/src/widgets/kernel/qwidgetwindow.cpp @@ -506,6 +506,11 @@ void QWidgetWindow::handleNonClientAreaMouseEvent(QMouseEvent *e) void QWidgetWindow::handleMouseEvent(QMouseEvent *event) { Q_D(QWidgetWindow); + + // Event delivery can potentially result in window re-creation (QTBUG-132912) + // so we need QPointer to avoid a dangling d below + QPointer self = this; + if (auto *activePopupWidget = QApplication::activePopupWidget()) { QPointF mapped = event->position(); if (activePopupWidget != m_widget) @@ -666,6 +671,9 @@ void QWidgetWindow::handleMouseEvent(QMouseEvent *event) event->setAccepted(translated.isAccepted()); } + if (self.isNull()) + return; + #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) if ( #else