diff --git a/src/corelib/kernel/qpermissions_darwin.mm b/src/corelib/kernel/qpermissions_darwin.mm index ae2cb2c423c..ed45899543a 100644 --- a/src/corelib/kernel/qpermissions_darwin.mm +++ b/src/corelib/kernel/qpermissions_darwin.mm @@ -39,13 +39,6 @@ QPermissionPlugin *permissionPlugin(const QPermission &permission) qCDebug(lcPermissions) << "Found matching plugin" << qUtf8Printable(className); auto *plugin = static_cast(pluginLoader()->instance(i)); if (!plugin->parent()) { - // We want to re-parent the plugin to the factory loader, so that it's - // cleaned up properly. To do so we first need to move the plugin to the - // same thread as the factory loader, as the plugin might be instantiated - // on a secondary thread if triggered from a checkPermission call (which - // is allowed on any thread). - plugin->moveToThread(pluginLoader->thread()); - // Also, as setParent will involve sending a ChildAdded event to the parent, // we need to make the call on the same thread as the parent lives, as events // are not allowed to be sent to an object owned by another thread. diff --git a/src/corelib/plugin/qfactoryloader.cpp b/src/corelib/plugin/qfactoryloader.cpp index daf68c5d18a..c1c4812044c 100644 --- a/src/corelib/plugin/qfactoryloader.cpp +++ b/src/corelib/plugin/qfactoryloader.cpp @@ -256,9 +256,9 @@ class QFactoryLoaderPrivate : public QObjectPrivate public: QFactoryLoaderPrivate() { } QByteArray iid; + mutable QMutex mutex; #if QT_CONFIG(library) ~QFactoryLoaderPrivate(); - mutable QMutex mutex; QDuplicateTracker loadedPaths; std::vector libraries; std::map keyMap; @@ -558,20 +558,25 @@ QObject *QFactoryLoader::instance(int index) const if (index < 0) return nullptr; -#if QT_CONFIG(library) QMutexLocker lock(&d->mutex); + QObject *obj = instanceHelper_locked(index); + + if (obj && !obj->parent()) + obj->moveToThread(QCoreApplicationPrivate::mainThread()); + return obj; +} + +inline QObject *QFactoryLoader::instanceHelper_locked(int index) const +{ + Q_D(const QFactoryLoader); + +#if QT_CONFIG(library) if (size_t(index) < d->libraries.size()) { QLibraryPrivate *library = d->libraries[index].get(); - if (QObject *obj = library->pluginInstance()) { - if (!obj->parent()) - obj->moveToThread(QCoreApplicationPrivate::mainThread()); - return obj; - } - return nullptr; + return library->pluginInstance(); } // we know d->libraries.size() <= index <= numeric_limits::max() → no overflow index -= static_cast(d->libraries.size()); - lock.unlock(); #endif QLatin1StringView iid(d->iid.constData(), d->iid.size()); diff --git a/src/corelib/plugin/qfactoryloader_p.h b/src/corelib/plugin/qfactoryloader_p.h index 7baaa2548a6..ae6d6279183 100644 --- a/src/corelib/plugin/qfactoryloader_p.h +++ b/src/corelib/plugin/qfactoryloader_p.h @@ -62,6 +62,9 @@ public: MetaDataList metaData() const; QList metaDataKeys() const; QObject *instance(int index) const; + +private: + inline QObject *instanceHelper_locked(int index) const; }; template