修复搜索插件生命周期管理内存泄漏问题

This commit is contained in:
iaom 2023-05-04 14:11:01 +08:00
parent f5eaf99f6f
commit df3332046f
4 changed files with 6 additions and 22 deletions

View File

@ -37,11 +37,6 @@ PluginManager *PluginManager::getInstance()
return global_instance; return global_instance;
} }
void PluginManager::setPluginEnableByName(const QString &pluginName, bool enable)
{
m_hash.value(pluginName)->setEnable(enable);
}
PluginManager::PluginManager(QObject *parent) : QObject(parent) PluginManager::PluginManager(QObject *parent) : QObject(parent)
{ {
QDir pluginsDir(PLUGIN_INSTALL_DIRS); QDir pluginsDir(PLUGIN_INSTALL_DIRS);
@ -73,12 +68,11 @@ PluginManager::PluginManager(QObject *parent) : QObject(parent)
PluginInterface *piface = dynamic_cast<PluginInterface*>(plugin); PluginInterface *piface = dynamic_cast<PluginInterface*>(plugin);
if (!piface) if (!piface)
continue; continue;
m_hash.insert(piface->name(), piface);
switch (piface->pluginType()) { switch (piface->pluginType()) {
case PluginInterface::PluginType::SearchPlugin: { case PluginInterface::PluginType::SearchPlugin: {
auto p = dynamic_cast<SearchPluginIface *>(plugin); auto p = dynamic_cast<SearchPluginIface *>(plugin);
if(!SearchPluginManager::getInstance()->registerExternalPlugin(p, pluginsDir.absoluteFilePath(fileName))) { if(!SearchPluginManager::getInstance()->registerExternalPlugin(p, pluginsDir.absoluteFilePath(fileName))) {
m_hash.erase(m_hash.find(piface->name())); delete p;
} }
break; break;
} }
@ -96,6 +90,4 @@ PluginManager::PluginManager(QObject *parent) : QObject(parent)
PluginManager::~PluginManager() PluginManager::~PluginManager()
{ {
m_hash.clear();
SearchPluginManager::getInstance()->close();
} }

View File

@ -41,16 +41,11 @@ public:
Q_SIGNALS: Q_SIGNALS:
void pluginStateChanged(const QString &pluginName, bool enable); void pluginStateChanged(const QString &pluginName, bool enable);
public Q_SLOTS:
void setPluginEnableByName(const QString &pluginName, bool enable);
private: private:
explicit PluginManager(QObject *parent = nullptr); explicit PluginManager(QObject *parent = nullptr);
~PluginManager(); ~PluginManager();
QHash<QString, PluginInterface*> m_hash;
}; };
} }

View File

@ -58,6 +58,10 @@ bool SearchPluginManager::registerPlugin(UkuiSearch::SearchPluginIface *plugin)
if (m_map.end() != m_map.find(plugin->name())) { if (m_map.end() != m_map.find(plugin->name())) {
qWarning() << "the plugin:" << plugin->name() << "has been registered."; qWarning() << "the plugin:" << plugin->name() << "has been registered.";
if (plugin) {
delete plugin;
plugin = nullptr;
}
return res; return res;
} }
@ -440,11 +444,6 @@ SearchPluginIface *SearchPluginManager::getPlugin(const QString &pluginId)
return m_map[pluginId]; return m_map[pluginId];
} }
void SearchPluginManager::close()
{
this->deleteLater();
}
SearchPluginManager::~SearchPluginManager() SearchPluginManager::~SearchPluginManager()
{ {
for (auto iter = m_map.begin(); iter != m_map.end();) { for (auto iter = m_map.begin(); iter != m_map.end();) {

View File

@ -56,8 +56,6 @@ public:
const QList<PluginInfo> getPluginIds(); const QList<PluginInfo> getPluginIds();
SearchPluginIface *getPlugin(const QString &pluginId); SearchPluginIface *getPlugin(const QString &pluginId);
void close();
private: private:
explicit SearchPluginManager(QObject *parent = nullptr); explicit SearchPluginManager(QObject *parent = nullptr);
~SearchPluginManager(); ~SearchPluginManager();