From 3091a3f3a45809af7d35a154d65210cb650db844 Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Thu, 13 Aug 2020 13:01:23 +0200 Subject: Native style: clean-up QStyle using a post routine MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When we delete QStyle, it will free up it's own internal resources. Especially on macOS, this means releasing a lot of NSViews and NSCells from the QMacStyle destructor. If we did this from ~QtQuickControls2NativeStylePlugin, it would happen when the plugin was unloaded from a Q_DESTRUCTOR_FUNCTION in QLibrary, which is very late in the tear-down process, and after qGuiApp has been set to nullptr, NSApplication has stopped running, and perhaps also other static platform variables (e.g in AppKit?) has been deleted. And to our best guess, this is also why we see a crash in AppKit from the destructor in QMacStyle. So for this reason, we delete QStyle from a post routine rather than from the destructor. Change-Id: I9dfb0d3394f14e5cd8b88d5a5fbbf3b73284faf1 Reviewed-by: Tor Arne Vestbø --- .../qtquickcontrols2nativestyleplugin.cpp | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/imports/nativestyle/qtquickcontrols2nativestyleplugin.cpp b/src/imports/nativestyle/qtquickcontrols2nativestyleplugin.cpp index 8aedcdb8..fe9deaab 100644 --- a/src/imports/nativestyle/qtquickcontrols2nativestyleplugin.cpp +++ b/src/imports/nativestyle/qtquickcontrols2nativestyleplugin.cpp @@ -36,6 +36,7 @@ #include #include +#include #include "qquicknativestyle.h" #include "qquickcommonstyle.h" @@ -63,8 +64,28 @@ public: QString name() const override; }; +static void deleteQStyle() +{ + // When we delete QStyle, it will free up it's own internal resources. Especially + // on macOS, this means releasing a lot of NSViews and NSCells from the QMacStyle + // destructor. If we did this from ~QtQuickControls2NativeStylePlugin, it would + // happen when the plugin was unloaded from a Q_DESTRUCTOR_FUNCTION in QLibrary, + // which is very late in the tear-down process, and after qGuiApp has been set to + // nullptr, NSApplication has stopped running, and perhaps also other static platform + // variables (e.g in AppKit?) has been deleted. And to our best guess, this is also why + // we see a crash in AppKit from the destructor in QMacStyle. So for this reason, we + // delete QStyle from a post routine rather than from the destructor. + QQuickNativeStyle::setStyle(nullptr); +} + QtQuickControls2NativeStylePlugin::~QtQuickControls2NativeStylePlugin() { + if (!qGuiApp) + return; + + // QGuiApplication is still running, so we need to remove the post + // routine to not be called after we have been unloaded. + qRemovePostRoutine(deleteQStyle); QQuickNativeStyle::setStyle(nullptr); } @@ -106,6 +127,8 @@ void QtQuickControls2NativeStylePlugin::initializeEngine(QQmlEngine *engine, con #endif } } + + qAddPostRoutine(deleteQStyle); QQuickNativeStyle::setStyle(style); } -- cgit v1.2.3