@@ -33,6 +33,12 @@ ModulesLoader::~ModulesLoader()
3333{
3434 quit ();
3535 wait ();
36+
37+ QMutexLocker locker (&m_mutex);
38+ for (auto &loader : m_pluginLoaders) {
39+ cleanupPluginLoader (loader);
40+ }
41+ m_pluginLoaders.clear ();
3642}
3743
3844ModulesLoader& ModulesLoader::instance ()
@@ -78,7 +84,7 @@ void ModulesLoader::findModule(const QString& path)
7884 }
7985
8086 qCInfo (DDE_SHELL) << " About to process " << module ;
81- auto loader = new QPluginLoader (path);
87+ auto loader = std::unique_ptr<QPluginLoader>( new QPluginLoader (path) );
8288
8389 // 检查兼容性
8490 const QJsonObject meta = loader->metaData ().value (" MetaData" ).toObject ();
@@ -134,7 +140,8 @@ void ModulesLoader::findModule(const QString& path)
134140 loader->moveToThread (qApp->thread ());
135141
136142 QMutexLocker locker (&m_mutex);
137- m_pluginLoaders.insert (moduleInstance->key (), QPointer<QPluginLoader>(loader));
143+ m_pluginLoaders.insert (moduleInstance->key (), QPointer<QPluginLoader>(loader.get ()));
144+ loader.release (); // 释放所有权,防止 QPluginLoader 被析构
138145 PluginManager::instance ()->addPlugin (moduleInstance, version);
139146 }
140147 m_loadLoginModule = true ;
@@ -268,6 +275,7 @@ bool ModulesLoader::isPluginEnabled(const QFileInfo& module)
268275 auto dbusInterface = new QDBusInterface (service, targetPath, interface, dbusConnection);
269276 if (!dbusInterface || !dbusInterface->isValid ()) {
270277 qCWarning (DDE_SHELL) << " Check plugin enabled dbus interface is not valid." ;
278+ dbusInterface->deleteLater ();
271279 return false ;
272280 }
273281 const bool pluginEnabled = dbusInterface->property (property.toStdString ().c_str ()).toBool ();
@@ -346,6 +354,14 @@ void ModulesLoader::unloadPlugin(const QString& path)
346354 }
347355}
348356
357+ void ModulesLoader::cleanupPluginLoader (QPluginLoader* loader)
358+ {
359+ if (loader) {
360+ loader->unload ();
361+ loader->deleteLater ();
362+ }
363+ }
364+
349365// TODO 只重新加载/卸载变更的插件
350366void ModulesLoader::onDConfigPropertyChanged (const QString& key, const QVariant& value, QObject* objPtr)
351367{
@@ -367,4 +383,4 @@ void ModulesLoader::onDbusPropertiesChanged(const QString& interfaceName, const
367383 start (QThread::LowestPriority);
368384 }
369385 }
370- }
386+ }
0 commit comments