Skip to content

Commit a9fc317

Browse files
committed
fix: avoid memory leak
as title Log: as title Pms: BUG-318013
1 parent 6817b6e commit a9fc317

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/global_util/plugin_manager/modules_loader.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3844
ModulesLoader& 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 只重新加载/卸载变更的插件
350366
void 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+
}

src/global_util/plugin_manager/modules_loader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class ModulesLoader : public QThread
4040
bool isPluginEnabled(const QFileInfo &module);
4141
bool contains(const QString &pluginFile) const;
4242
QPair<QString, QPluginLoader*> getPluginLoader(const QString &pluginFile) const;
43+
void cleanupPluginLoader(QPluginLoader* loader);
4344

4445
static void onDConfigPropertyChanged(const QString &key, const QVariant &value, QObject *objPtr);
4546

0 commit comments

Comments
 (0)