OSDN Git Service

do not cache the QGuiPlatformPlugin instance from qt_guiPlatformPlugin()
authorIvailo Monev <xakepa10@gmail.com>
Sun, 20 Aug 2023 23:35:15 +0000 (02:35 +0300)
committerIvailo Monev <xakepa10@gmail.com>
Sun, 20 Aug 2023 23:35:15 +0000 (02:35 +0300)
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 <xakepa10@gmail.com>
src/gui/kernel/qguiplatformplugin.cpp

index e724954..6ec624b 100644 (file)
@@ -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<QGuiPlatformPlugin*>(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<QGuiPlatformPlugin*>(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;
 }