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 <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2023-08-21 02:35:15 +03:00
parent 0140df36db
commit 0d219c845c

View file

@ -46,33 +46,32 @@ Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, platformLoader, (QString::fromLatin1("
*/ */
QGuiPlatformPlugin *qt_guiPlatformPlugin() QGuiPlatformPlugin *qt_guiPlatformPlugin()
{ {
static QGuiPlatformPlugin *plugin = nullptr; QGuiPlatformPlugin *plugin = nullptr;
if (!plugin) {
#ifndef QT_NO_LIBRARY #ifndef QT_NO_LIBRARY
static const char* platformEnvTbl[] = { static const char* platformEnvTbl[] = {
"QT_PLATFORM_PLUGIN", "QT_PLATFORM_PLUGIN",
"XDG_CURRENT_DESKTOP", "XDG_CURRENT_DESKTOP",
"DESKTOP_SESSION", "DESKTOP_SESSION",
nullptr nullptr
}; };
int counter = 0; int counter = 0;
while (platformEnvTbl[counter]) { while (platformEnvTbl[counter]) {
QString key = qGetEnv(platformEnvTbl[counter]); QString key = qGetEnv(platformEnvTbl[counter]);
if (!key.isEmpty()) { if (!key.isEmpty()) {
plugin = qobject_cast<QGuiPlatformPlugin*>(platformLoader()->instance(key)); plugin = qobject_cast<QGuiPlatformPlugin*>(platformLoader()->instance(key));
if (plugin) { if (plugin) {
break; break;
}
} }
counter++;
} }
counter++;
}
#endif // QT_NO_LIBRARY #endif // QT_NO_LIBRARY
if (!plugin) { if (!plugin) {
static QGuiPlatformPlugin def; static QGuiPlatformPlugin def;
plugin = &def; plugin = &def;
}
} }
return plugin; return plugin;
} }