diff options
author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2020-08-13 13:01:23 +0200 |
---|---|---|
committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2020-08-20 10:19:54 +0000 |
commit | 3091a3f3a45809af7d35a154d65210cb650db844 (patch) | |
tree | 1aac0689004c3b1586b1168de5b7c68817eec55d | |
parent | ab95b3ca1187347cb4b3bc0a1d01886091e3b2a6 (diff) |
Native style: clean-up QStyle using a post routine
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ø <tor.arne.vestbo@qt.io>
-rw-r--r-- | src/imports/nativestyle/qtquickcontrols2nativestyleplugin.cpp | 23 |
1 files changed, 23 insertions, 0 deletions
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 <QtQml/qqml.h> #include <QtQuickControls2/private/qquickstyleplugin_p.h> +#include <QtGui/qguiapplication.h> #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); } |