From 8e74bf4d4aaaf582c70cbcb83caa237e2592e762 Mon Sep 17 00:00:00 2001 From: Eirik Aavitsland Date: Fri, 7 Feb 2020 16:00:00 +0100 Subject: [PATCH] Avoid dangling QGuiApplicationPrivate pointer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The static self pointer of QGuiApplicationPrivate was not reset at destruction (in constrast to the corresponding QGuiApplication::self). This could cause crashes when calling Qt API after QGuiApplication destruction. Fixing this revealed an issue with QGuiApplication::font(), which would assert QGuiApplicationPrivate::self. But the QApplication autotest actually calls this function with no QApplication instance. That autotest passes only coincidentally, since another QApplication instance has been created and deleted already, and the dangling self pointer of that instance was never reset. To improve the robustness of the api, replace the assert/crash with just a warning and return an "empty" QFont. (The assert was added for 5.0 for QTBUG-28306 in order to give a nicer warning when mixing QWidget and QtCore/GuiApplication. However it never got that effect in practice, since that issue was fixed at the same time by another, better patch for the duplicate bug QTBUG-28076). Fixes: QTBUG-81954 Change-Id: I3fa6cad1625a3e70631b5170d53119d63492b534 Reviewed-by: Tor Arne Vestbø --- src/gui/kernel/qguiapplication.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index 1190bd1936..67b1ed15c5 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -1727,6 +1727,8 @@ QGuiApplicationPrivate::~QGuiApplicationPrivate() window_list.clear(); screen_list.clear(); + + self = nullptr; } #if 0 @@ -3401,8 +3403,11 @@ void QGuiApplicationPrivate::applyWindowGeometrySpecificationTo(QWindow *window) */ QFont QGuiApplication::font() { - Q_ASSERT_X(QGuiApplicationPrivate::self, "QGuiApplication::font()", "no QGuiApplication instance"); const auto locker = qt_scoped_lock(applicationFontMutex); + if (!QGuiApplicationPrivate::self && !QGuiApplicationPrivate::app_font) { + qWarning("QGuiApplication::font(): no QGuiApplication instance and no application font set."); + return QFont(); // in effect: QFont((QFontPrivate*)nullptr), so no recursion + } initFontUnlocked(); return *QGuiApplicationPrivate::app_font; }