title: Create instance of a class within a method of another class
body: <p>this is probably a really basic C++ question but I simply cannot find out a way to properly do this:</p>
  
  <p>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. The QPluginLoader instances have to be available throughout the whole GUI class.
  But whenever I try to access a QPluginLoader through my QList the program crashes. 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.</p>
  
  <p>But how do I make the instance available in the whole class?</p>
  
  <p>Here is what I tried:</p>
  
  <p>mainwindow.h</p>
  
  <pre><code>class MainWindow : public QMainWindow
  {
      Q_OBJECT
  
  public slots:
      void onThreadTerminated();
  
  private:
      bool loadPlugins();
      QList&lt;QPluginLoader*&gt; loaderList;
  };
  </code></pre>
  
  <p>mainwindow.cpp</p>
  
  <pre><code>bool MainWindow::loadPlugins()
  {
      QDir pluginsDir(qApp-&gt;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(&amp;pluginLoader);
              pluginCount++;
          }
      }
      return false;
  }
  </code></pre>
  
  <p>last but not least, the slot which crashes my program</p>
  
  <pre><code>// SLOT
  void MainWindow::onThreadTerminated()
  {
      while (!loaderList.isEmpty()) {
         if(loaderList.takeFirst()-&gt;isLoaded()) loaderList.takeFirst()-&gt;unload();
      }
      loaderList.clear();
  }
  </code></pre>