Crash on App Exit with Plugins



  • Following the Plug & Paint example, I have created a working application that exploits plugins. However, when I exit my application, I get the message that the program has unexpectedly finished. I have identified the error to be caused by using the QObject returned by QPluginLoader::instance() as second argument in the constructor for QAction as also used in the MainWindow::addToMenu() function of the Plug & Paint example. I have however condensed my code a bit compared to this example:

    foreach (QString File, PluginLocation.entryList(QDir::Files)) {
        if (QLibrary::isLibrary(File)) {
            QPluginLoader Loader(PluginLocation.absoluteFilePath(File));
            QObject *Plugin = Loader.instance();
            if (Plugin) {
                Importer *ImportPlugin = qobject_cast<Importer *>(Plugin);
                if (ImportPlugin) {
                    QAction *ImportAction = new QAction(ImportPlugin->menu() + "...", Plugin); // The second argument causes the crash on exit
                    connect(ImportAction, SIGNAL(triggered()), this, SLOT(onImport()));
                    ImportMenu->addAction(ImportAction);
                }
            }
        }
    }
    

    Any ideas where I should be looking to solve the crashing behavior?



  • I found a solution (although I am not sure whether this is the way to go): When loading the plugins as above, I am now storing the corresponding actions in a list. Upon closing the application, I first delete all actions in this list. This approach resolves the crashing behavior, but if anybody has a more appropriate solution, please let me know :)


  • Lifetime Qt Champion

    Hi,

    Out of curiosity, why are you setting the plugin as parent of your QAction ?



  • That's how it is done in the Plug & Paint example and the reason is that one has access to the involved plugin for further processing when the action is performed. And just like the Plug & Paint example, I need this access ;)


  • Lifetime Qt Champion

    What version of Qt are you using ?



  • This is with Qt 5.7.1


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.