简体   繁体   English

在另一个类的方法中创建类的实例

[英]Create instance of a class within a method of another class

this is probably a really basic C++ question but I simply cannot find out a way to properly do this: 这可能是一个非常基本的C ++问题,但我找不到正确的方法:

I need to create some instances of class "QPluginLoader" within a method of my GUI class and want to store these instances within a QList. 我需要在GUI类的方法中创建类“QPluginLoader”的一些实例,并希望将这些实例存储在QList中。 The QPluginLoader instances have to be available throughout the whole GUI class. QPluginLoader实例必须在整个GUI类中可用。 But whenever I try to access a QPluginLoader through my QList the program crashes. 但每当我尝试通过我的QList访问QPluginLoader时程序崩溃。 I assume that is because once out of the scope where I instantiated the QPluginLoader class, it is destroyed, so it's not available outside of the method where I created the QPluginLoader and stored it into the QList. 我假设这是因为一旦超出我实例化QPluginLoader类的范围,它就会被销毁,所以它在我创建QPluginLoader并将其存储到QList的方法之外不可用。

But how do I make the instance available in the whole class? 但是如何在全班中提供实例?

Here is what I tried: 这是我尝试过的:

mainwindow.h mainwindow.h

class MainWindow : public QMainWindow
{
    Q_OBJECT

public slots:
    void onThreadTerminated();

private:
    bool loadPlugins();
    QList<QPluginLoader*> loaderList;
};

mainwindow.cpp mainwindow.cpp

bool MainWindow::loadPlugins()
{
    QDir pluginsDir(qApp->applicationDirPath());
    pluginsDir.cd("plugins");

    foreach (QString fileName, pluginsDir.entryList(QDir::Files))
    {
        QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
        QObject* plugin = pluginLoader.instance();

        if (plugin)
        {
            loaderList.push_back(&pluginLoader);
            pluginCount++;
        }
    }
    return false;
}

last but not least, the slot which crashes my program 最后但并非最不重要的是,崩溃我的程序的插槽

// SLOT
void MainWindow::onThreadTerminated()
{
    while (!loaderList.isEmpty()) {
       if(loaderList.takeFirst()->isLoaded()) loaderList.takeFirst()->unload();
    }
    loaderList.clear();
}
foreach (QString fileName, pluginsDir.entryList(QDir::Files))
{
    QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));

pluginLoader will be destroyed at the next } and you keep a reference of this destroyed object in loaderList . pluginLoader将在下一个被销毁}并且你在loaderList保留了这个被破坏对象的loaderList

Allocate memory for pluginLoader dynamically using new and keep pointers to QPluginLoader in loaderList 使用new动态分配pluginLoader内存,并在loaderList保留指向QPluginLoaderloaderList

Ofcourse you will have to remember to free the allocated memory for QPluginLoader s 当然,你必须记住为QPluginLoader释放分配的内存

The problem is these two lines in the loadPlugins function 问题是loadPlugins函数中的这两行

QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
...
loaderList.push_back(&pluginLoader);

The first define the local object pluginLoader . 第一个定义本地对象pluginLoader The second pushes a pointer to that local object into the container. 第二个将指向该本地对象的指针推入容器中。

The problem is that when the loop iterates (or ends), the life-time of pluginLoader ends and it is destructed, the pointer to it becomes invalid. 问题是当循环迭代(或结束)时, pluginLoader的生命pluginLoader结束并且它被破坏,指向它的指针变为无效。

It seems to me that you should not have a container of plugin loaders, but of the actual plugins themselves. 在我看来,你不应该有一个插件加载器的容器,而是实际的插件本身。

If you really want the plugin loader in the container, you need to create those dynamically with new , or at least use smart pointers for it. 如果你真的想在容器中的插件加载器,你需要用动态的创建这些new ,或至少使用智能指针它。 Or, as mentioned in a comment to your question, use a container of QPluginLoader objects . 或者,如您对问题的评论中所述,使用QPluginLoader 对象的容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM