ロードされたプラグインをアンロードして新しいプラグインをロードしようとすると、問題が発生します。したがって、両方のプラグインは正しくロードされますが、それらを切り替えると(最初のプラグインがロードされ、2番目のプラグインがアンロードされ、その逆)、アプリがクラッシュします。何が問題になる可能性がありますか?
まず、QPluginLoaderのQListに保存されているプラグインをアンロードしようとします。次に、ロードするプラグインを確認します(プラグインをロードするための特別なメニューから渡されたid(整数)に依存します)。最初のロードは正常です(最初のプラグインがロードされ、この時点でアンロードするものはありません)、2番目のロード(最初のプラグインをアンロードし、2番目がロードされます)、3番目のロードでクラッシュします
void MainWindow::loadPluginUsingId (int plugin_id) {
foreach (QPluginLoader* pluginLoader, plugins) {
pluginLoader->unload();
delete pluginLoader;
}
switch (plugin_id) {
case 0 : {
foreach (QString fileName, pluginDir.entryList(QDir::Files)) {
if (fileName == fullNameOfPlugins.value(plugin_id)) {
QPluginLoader* pluginLoader = new QPluginLoader(pluginDir.absoluteFilePath(fileName));
QObject *plugin = pluginLoader->instance();
IndicatorInterface *indicator = qobject_cast<IndicatorInterface*>(plugin);
indicator->initIndicator();
plugins.append(pluginLoader);
}
}
}
break;
case 1 : {
foreach (QString fileName, pluginDir.entryList(QDir::Files)) {
if (fileName == fullNameOfPlugins.value(plugin_id)) {
QPluginLoader* pluginLoader = new QPluginLoader(pluginDir.absoluteFilePath(fileName));
QObject* plugin = pluginLoader->instance();
PlotterInterface *plotter = qobject_cast<PlotterInterface*>(plugin);
plotter->initPlotter();
plugins.append(pluginLoader);
}
}
}
break;
default :
break;
}
}