title: QT Application segmentation fault on exit when using std::unique_ptr for the QMainWindow
body: <p>I should clarify first, my question is, why does the segfault happen in the case where I use a <code>std::unique_ptr</code> but it doesn't when I change the code to use new and delete? I don't really have an issue here, since the segfault is fixed and I know I'm properly cleaning things up with new and delete. I just want to know why I couldn't use a <code>std::unique_ptr</code>.</p>
  
  <p>If I create my <code>QMainWindow</code> instance in a <code>std::unique_ptr</code> and my <code>MainWindow</code> creates a child window (e.g. opening a <code>QComboBox</code> or <code>ToolTip</code>), my application will segfault on exit. In this case, if my <code>MainWindow</code> does not create any child windows, there is no segfault.</p>
  
  <p>If I manage my <code>QMainWindow</code> instance myself with new and delete, there is no segfault regardless of whether or not I create a child window.</p>
  
  <p>This is my main, it simply calls <code>run</code> on my <code>Bridge</code> class that handles creating the instance of my <code>MainWindow</code> class and starting it.</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
      Bridge bridge(argc, argv);
      bridge.run(); // Handles starting the main window
  }
  </code></pre>
  
  <p><strong>This is a shortened version of my <code>Bridge</code> class that results in a segfault (using std::unique_ptr I get a segfault on exit):</strong></p>
  
  <pre><code>class Bridge::IMPL {
  public:
      IMPL(int&amp; argc, char ** argv) : 
          mainwindow(), isRunning(true), app(argc, argv) {}
      ~IMPL() = default;
  public:
      std::unique_ptr&lt;MainWindow&gt; mainwindow;
      bool isRunning;
      QApplication app;
  };
  
  Bridge::Bridge(int&amp; argc, char ** argv) :
      pImpl(make_unique&lt;IMPL&gt;(argc, argv)) {
  
      pImpl-&gt;mainwindow.reset(new MainWindow(this));
  }
  </code></pre>
  
  <p><strong>This is the Bridge class with no segfault (slightly modified to remove the <code>std::unique_ptr</code> and instead use new and delete. This version of the <code>Bridge</code> class DOES NOT CRASH):</strong></p>
  
  <pre><code>class Bridge::IMPL {
  public:
      IMPL(int&amp; argc, char ** argv) : 
          mainwindow(nullptr), isRunning(true), app(argc, argv) {}
      ~IMPL() {
          if (mainwindow) {
              delete mainwindow;
          }
      }
  public:
      MainWindow* mainwindow;
      bool isRunning;
      QApplication app;
  };
  
  Bridge::Bridge(int&amp; argc, char ** argv) :
      pImpl(make_unique&lt;IMPL&gt;(argc, argv)) {
  
      pImpl-&gt;mainwindow= new MainWindow(this);
  }
  </code></pre>
  
  <p><strong>This is the back-trace when a segfault would happen:</strong></p>
  
  <p><em>Also there is a weird print-out about <code>QBasicTimer</code> that doesn't get printed after I changed the code to use new and delete instead of <code>std::unique_ptr</code></em></p>
  
  <pre><code>QBasicTimer::start: QBasicTimer can only be used with threads started with QThread
  
  Thread 1 "application" received signal SIGSEGV, Segmentation fault.
  0x00007fffe75bf2e2 in ?? () from /home/user/Qt/5.9/gcc_64/plugins/platforms/../../lib/libQt5XcbQpa.so.5
  (gdb) bt
  #0  0x00007fffe75bf2e2 in ?? () from /home/user/Qt/5.9/gcc_64/plugins/platforms/../../lib/libQt5XcbQpa.so.5
  #1  0x00007fffe75bf5c4 in ?? () from /home/user/Qt/5.9/gcc_64/plugins/platforms/../../lib/libQt5XcbQpa.so.5
  #2  0x00007fffe75b9669 in QXcbConnection::removeWindowEventListener(unsigned int) () from /home/user/Qt/5.9/gcc_64/plugins/platforms/../../lib/libQt5XcbQpa.so.5
  #3  0x00007fffe75ceafa in QXcbWindow::destroy() () from /home/user/Qt/5.9/gcc_64/plugins/platforms/../../lib/libQt5XcbQpa.so.5
  #4  0x00007fffe75cec07 in QXcbWindow::~QXcbWindow() () from /home/user/Qt/5.9/gcc_64/plugins/platforms/../../lib/libQt5XcbQpa.so.5
  #5  0x00007fffe43ce2ee in ?? () from /home/user/Qt/5.9/gcc_64/plugins/xcbglintegrations/libqxcb-glx-integration.so
  #6  0x00007ffff4b56f46 in QWindowPrivate::destroy() () from /home/user/Qt/5.9/gcc_64/lib/libQt5Gui.so.5
  #7  0x00007ffff534ecd7 in QWidgetPrivate::deleteTLSysExtra() () from /home/user/Qt/5.9/gcc_64/lib/libQt5Widgets.so.5
  #8  0x00007ffff53524d8 in QWidget::destroy(bool, bool) () from /home/user/Qt/5.9/gcc_64/lib/libQt5Widgets.so.5
  #9  0x00007ffff53598b0 in QWidget::~QWidget() () from /home/user/Qt/5.9/gcc_64/lib/libQt5Widgets.so.5
  #10 0x00007ffff541ae7a in ?? () from /home/user/Qt/5.9/gcc_64/lib/libQt5Widgets.so.5
  #11 0x00007ffff4580b83 in QObjectPrivate::deleteChildren() () from /home/user/Qt/5.9/gcc_64/lib/libQt5Core.so.5
  #12 0x00007ffff5359894 in QWidget::~QWidget() () from /home/user/Qt/5.9/gcc_64/lib/libQt5Widgets.so.5
  #13 0x00007ffff540eac9 in QComboBox::~QComboBox() () from /home/user/Qt/5.9/gcc_64/lib/libQt5Widgets.so.5
  #14 0x00007ffff4580b83 in QObjectPrivate::deleteChildren() () from /home/user/Qt/5.9/gcc_64/lib/libQt5Core.so.5
  #15 0x00007ffff5359894 in QWidget::~QWidget() () from /home/user/Qt/5.9/gcc_64/lib/libQt5Widgets.so.5
  #16 0x00007ffff5359ab9 in QWidget::~QWidget() () from /home/user/Qt/5.9/gcc_64/lib/libQt5Widgets.so.5
  #17 0x00007ffff4580b83 in QObjectPrivate::deleteChildren() () from /home/user/Qt/5.9/gcc_64/lib/libQt5Core.so.5
  #18 0x00007ffff5359894 in QWidget::~QWidget() () from /home/user/Qt/5.9/gcc_64/lib/libQt5Widgets.so.5
  #19 0x00007ffff5359ab9 in QWidget::~QWidget() () from /home/user/Qt/5.9/gcc_64/lib/libQt5Widgets.so.5
  #20 0x00007ffff4580b83 in QObjectPrivate::deleteChildren() () from /home/user/Qt/5.9/gcc_64/lib/libQt5Core.so.5
  #21 0x00007ffff5359894 in QWidget::~QWidget() () from /home/user/Qt/5.9/gcc_64/lib/libQt5Widgets.so.5
  #22 0x0000000000421ed4 in MainWindow::~MainWindow() ()
  #23 0x0000000000421f0e in MainWindow::~MainWindow() ()
  #24 0x0000000000421698 in std::default_delete&lt;MainWindow&gt;::operator()(MainWindow*) const ()
  #25 0x0000000000421171 in std::unique_ptr&lt;MainWindow, std::default_delete&lt;MainWindow&gt; &gt;::~unique_ptr() ()
  #26 0x000000000042191c in Bridge::IMPL::~IMPL() ()
  #27 0x0000000000421942 in std::default_delete&lt;Bridge::IMPL&gt;::operator()(Bridge::IMPL*) const ()
  #28 0x0000000000421387 in std::unique_ptr&lt;Bridge::IMPL, std::default_delete&lt;Bridge::IMPL&gt; &gt;::~unique_ptr() ()
  #29 0x0000000000420cb0 in Bridge::~Bridge() ()
  #30 0x0000000000421c41 in main ()
  </code></pre>
  
  <p>It should also be noted that this <code>QApplication</code> is run using a while loop and calls to <code>app.processEvents()</code> rather than calling <code>exec()</code>. I realize that's not the best way to do it, however this app is part of another application that polls for events from somewhere else and since the nature of this application is more "proof-of-concept" than anything else, I wanted to keep it simple by having it all in one thread. See below for a snippet of my run method:</p>
  
  <pre><code>while (isRunning()) {
      poller.poll(25);
      app.processEvents();
  }
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>In my constructor for <code>MainWindow</code> the <code>this</code> I'm passing is not being set as the parent (the parent is <code>nullptr</code>):</p>
  
  <p>header:</p>
  
  <pre><code>class MainWindow: public QMainWindow
  {
      Q_OBJECT
  public:
      class BridgeInterface{
      public:
          // Some pure virtual methods here
      };
  
      explicit MainWindow(BridgeInterface* interface, QWidget* parent = nullptr);
  
  // some more stuff...
  
  private:
      class IMPL;
      std::unique_ptr&lt;IMPL&gt; pImpl;
  };
  </code></pre>
  
  <p>source:</p>
  
  <pre><code>MainWindow::MainWindow(BridgeInterface* interface, QWidget* parent) :
      QMainWindow(parent), pImpl(make_unique&lt;IMPL&gt;()) {
  
      pImpl-&gt;ui-&gt;setupUi(this);
      pImpl-&gt;bridgeInterface = interface;
  
      // Connect slots and stuff
  }
  </code></pre>