commontk / CTK

A set of common support code for medical imaging, surgical navigation, and related purposes.
https://commontk.org
Apache License 2.0
827 stars 480 forks source link

How to appropriately use ctkPluginFrameworkLauncher::stop() #1161

Open haomehaode opened 6 months ago

haomehaode commented 6 months ago

Dear CTK Developer, I am facing a strange problem as I quit a GUI app. I believe it is related to multiple threads. Here is a snapshot of the main function where the ctkPluginFramework is initialized. To test if all plugin could be removed once the stop function is called, a line is added as follows,

QApplication app(argc, argv);

ctkPluginFrameworkLauncher::addSearchPath(QCoreApplication::applicationDirPath() + "/plugins");
bool success = ctkPluginFrameworkLauncher::start("org.commontk.eventadmin");
ctkPluginContext* context = ctkPluginFrameworkLauncher::getPluginContext();

QString path = QCoreApplication::applicationDirPath() + "/plugins";
QDirIterator itPlugin(path, QStringList() << "*.dll" << "*.so", QDir::Files);
while (itPlugin.hasNext()) {
    QString strPlugin = itPlugin.next();
    try {
        QSharedPointer<ctkPlugin> plugin = context->installPlugin(QUrl::fromLocalFile(strPlugin));
        plugin->start(ctkPlugin::START_TRANSIENT);
        qDebug() << "Plugin start ..." << strPlugin;
    }
    catch (const ctkPluginException& e) {
        qDebug() << "Failed to install plugin" << e.what();
        return -1;
    }
}

ctkServiceReference reference = context->getServiceReference<IMainWindow>();
if (reference) {
    IMainWindow* service = qobject_cast<IMainWindow*>(context->getService(reference));
    if (service != Q_NULLPTR) {
        service->init();
    }
}

ctkPluginFrameworkLauncher::stop();

Here is a snapshot of what occur, with this I believe it is a multiple thread issue. I would like to take some advice on how(or where) to use ctkPluginFrameworkLauncher::stop().

244643815-fd532edb-df33-45db-afe7-744ccff8fb29

If I comment out the code

//ctkPluginFrameworkLauncher::stop();
Here is a snapshot of what occur 屏幕截图 2024-01-02 152417

I hope you can give me some suggestions,Thanks