PDA

View Full Version : new, but no delete



Windsoarer
10th December 2008, 20:42
For somebody coming to Qt after several years of programming with MFC, one thing which is surprising is that objects created on the heap with "new" don't seem to require a "delete" instruction.
For instance, in the tabdialog example provided with the Qt library, the statement
tabWidget = new QTabWidget;
isn't followed by a statement
delete tabWidget
in the destructor ~TabDialog()

In MFC, this would cause a memory leak. Isn't this the case in QT ?
Is there some kind of built-in garbage collector ?

Thanks in advance for any insight

sepehr
10th December 2008, 22:02
of course widgets should be deleted from the memory once they are not needed,widgets that have parents would be taken care of for deleting,and objects which don't have parents must be explicitly deleted,i remember creating bunch of Widgets but forgot to delete them and when exiting the program i got some segmentation fault errors

wysota
10th December 2008, 22:20
What sepehr wrote might be a bit misleading :-)

You don't need to call delete on widgets that have a parent (in terms of QObject parent-child relationship) yourself, because the parent object deletes all its children when it itself gets destroyed. Thus you only need to delete top-level, parentless windows that were created on heap. Everything else (I mean widgets) will get destroyed automagically. Cute, isn't it? :-)