title: Qt: Preventing memory leaks when closing windows
body: <p>All the example code I can find for Qt shows code that creates objects with new, but they don't get cleaned up in any window close situations. Unless Qt automatically destroys them, this seems false.</p>
  
  <p>Will Qt automatically destroy all the GUI objects in the window when it's closed, or am I responsible for that myself just like I would normally be for everything else?</p>
  
  <p>If I'm responsible for it, is there a "polite" way? Or can I just keep a vector of objects to destroy and run it in the reverse order they were created, calling delete on each?</p>
  
  <p>For instance, here's the example for using BorderLayout:</p>
  
  <pre><code>Window::Window()
  {
      QTextBrowser *centralWidget = new QTextBrowser;
      centralWidget-&gt;setPlainText(tr("Central widget"));
  
      BorderLayout *layout = new BorderLayout;
      layout-&gt;addWidget(centralWidget, BorderLayout::Center);
      layout-&gt;addWidget(createLabel("North"), BorderLayout::North);
      layout-&gt;addWidget(createLabel("West"), BorderLayout::West);
      layout-&gt;addWidget(createLabel("East 1"), BorderLayout::East);
      layout-&gt;addWidget(createLabel("East 2") , BorderLayout::East);
      layout-&gt;addWidget(createLabel("South"), BorderLayout::South);
      setLayout(layout);
  
      setWindowTitle(tr("Border Layout"));
  }
  </code></pre>
  
  <p>This code creates a layout and 6 widgets with new QTextBrowser or new QLabel. It doesn't even try to remember them for later destruction.</p>