title: Qt destructor call for closed widget
body: <p>There is application that handles text commands. I have a Qt widget that is closed with some <code>close *</code> command. <code>Qt::WA_DeleteOnClose</code> attribute is set for that widget, it receives closeEvent, but destructor for that object is called later (I guess on idle). If I have two commands
  <code>close *; get something;</code> the program crashes because <code>get something</code> is called before destructor for that widget, so it tries to access data deleted by <code>close *</code> command. How can I force Qt to call destructors? <code>QCoreApplication::processEvents()</code> after close command doesn't help.
  I've got this problem after changing qt version to 4.7.2 from 4.3.3. There is no multithreading here.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>added</strong></p>
  
  <p>Here is the code example.</p>
  
  <pre><code>test *t = new test();
  t-&gt;show();
  std::cout &lt;&lt; "before deleteLater()" &lt;&lt; std::endl;
  t-&gt;deleteLater();
  std::cout &lt;&lt; "after deleteLater()" &lt;&lt; std::endl;
  QCoreApplication::sendPostedEvents();
  QCoreApplication::processEvents();
  std::cout &lt;&lt; "after processEvents()" &lt;&lt; std::endl;
  </code></pre>
  
  <p>test class is derived from QDialog. It prints <code>test()</code> in constructor and <code>~test()</code> in destructor. This code gives the following output</p>
  
  <pre><code>test()
  before deleteLater()
  after deleteLater()
  after processEvents()
  ~test()
  </code></pre>
  
  <p>According to Qt documentation it should delete the object before last cout, am I right? Looks like a bug in Qt, does anybody know anything about it? Any workaround?</p>
  
  <p>I asked the question in Qt mailing list, but still waiting for an answer.</p>
  
  <p>Thanks.</p>
  
  <p><strong>one more update</strong></p>
  
  <p>This code</p>
  
  <pre><code>Dialog::~Dialog() {
      std::cout &lt;&lt; "~test()" &lt;&lt; std::endl;
  }
  
  int main(int argc, char* argv[]) {
      QApplication app(argc, argv);
      Dialog* dlg = new Dialog();
      dlg-&gt;setAttribute(Qt::WA_DeleteOnClose);
      dlg-&gt;show();
      dlg-&gt;close();
      std::cout &lt;&lt; "before sendPostedEvents()" &lt;&lt; std::endl;
      QCoreApplication::sendPostedEvents();
      std::cout &lt;&lt; "after sendPostedEvents()" &lt;&lt; std::endl;
      return app.exec();
  }
  </code></pre>
  
  <p>prints this</p>
  
  <pre><code>before sendPostedEvents()
  after sendPostedEvents()
  ~test()
  </code></pre>
  
  <p>but as soon as I add closeEvent handler and call deleteLater() in that handler function sendPostedEvents starts deleting deferred objects.</p>
  
  <pre><code>void Dialog::closeEvent(QCloseEvent* ev) {
      deleteLater();
      QWidget::closeEvent(ev);
  }
  </code></pre>
  
  <p>prints this
      before sendPostedEvents()
      ~test()
      after sendPostedEvents()</p>
  
  <p>Can anybody explain what the hell is going on there? Is it just a bug? Can I use that as a workaround?</p>
  
  <p>How does this work? Shouldn't Qt call deleteLater() automatically, after closeEvent is accepted if CloseOnDelete attribute is set? </p>