From 0d219c845cda3bd057e4cb4da28fb3fbbd1c389b Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Mon, 21 Aug 2023 02:35:15 +0300 Subject: [PATCH] do not cache the QGuiPlatformPlugin instance from qt_guiPlatformPlugin() plugin lookup is affected by several things including environment variables (QT_PLUGIN_PATH, QT_PLATFORM_PLUGIN, XDG_CURRENT_DESKTOP and DESKTOP_SESSION which can be changed at any time by a setenv() call) and plugin paths (QT_PLUGIN_PATH, QCoreApplication::pluginPaths() and the related QCoreApplication methods to change the plugin paths during runtime) so caching an instance of the plugin while beneficial is subject to not being able to load a plugin that may be available only after environment variable or plugin paths change Signed-off-by: Ivailo Monev --- src/gui/kernel/qguiplatformplugin.cpp | 41 +++++++++++++-------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/gui/kernel/qguiplatformplugin.cpp b/src/gui/kernel/qguiplatformplugin.cpp index e724954ee..6ec624b63 100644 --- a/src/gui/kernel/qguiplatformplugin.cpp +++ b/src/gui/kernel/qguiplatformplugin.cpp @@ -46,33 +46,32 @@ Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, platformLoader, (QString::fromLatin1(" */ QGuiPlatformPlugin *qt_guiPlatformPlugin() { - static QGuiPlatformPlugin *plugin = nullptr; - if (!plugin) { + QGuiPlatformPlugin *plugin = nullptr; #ifndef QT_NO_LIBRARY - static const char* platformEnvTbl[] = { - "QT_PLATFORM_PLUGIN", - "XDG_CURRENT_DESKTOP", - "DESKTOP_SESSION", - nullptr - }; - int counter = 0; - while (platformEnvTbl[counter]) { - QString key = qGetEnv(platformEnvTbl[counter]); - if (!key.isEmpty()) { - plugin = qobject_cast(platformLoader()->instance(key)); - if (plugin) { - break; - } + static const char* platformEnvTbl[] = { + "QT_PLATFORM_PLUGIN", + "XDG_CURRENT_DESKTOP", + "DESKTOP_SESSION", + nullptr + }; + int counter = 0; + while (platformEnvTbl[counter]) { + QString key = qGetEnv(platformEnvTbl[counter]); + if (!key.isEmpty()) { + plugin = qobject_cast(platformLoader()->instance(key)); + if (plugin) { + break; } - counter++; } + counter++; + } #endif // QT_NO_LIBRARY - if (!plugin) { - static QGuiPlatformPlugin def; - plugin = &def; - } + if (!plugin) { + static QGuiPlatformPlugin def; + plugin = &def; } + return plugin; }