So, I've learned so far, that Qt releases the memory of all child objects when a parent object gets deleted. Also, one generally doesn't have to care about memory management for objects created on the stack (i.e. NOT as pointers).
Now, when I did the very good "AddressBook" tutorial, I found this in part 5:
AddressBook::AddressBook(QWidget *parent) : QWidget(parent)
{
dialog = new FindDialog;
}
Complete source is available:
addressbook.h
addressbook.cpp
finddialog.h
Here, dialog
is a private member of AddressBook
, and it is a pointer to a FindDialog
. FindDialog
inherits QDialog
, but no this
-Pointer is passed to the constructor (as seen above). No explicit destructor exists, there is no delete dialog
-call...
Also, not passing this
seems to be intentional:
[The FindDialog's] constructor is defined to accept a parent QWidget, even though the dialog will be opened as a separate window.
Wouldn't this cause a memory leak? Or is there some other mechanism that will silently delete dialog
and free its memory?
Thanks in advance for any help!
Update: I posted this issue to the qt-project.org forums and it should get fixed soon.
There is no excuse for this, and it eventually has more issues than you just bring up, namely:
It is not managed as you say.
It does not use the conventional new Foo() syntax.
It is not done in the constructor's initializer list.
The OS will probably free this up once the application quits, but still, I always speak up against such issues, anyhow, especially in example projects. The appropriate fix would be to use either a stack object instead of the heap object or QPointer
in my opinion.
See the following post for details in case the latter:
How to crash (almost) every Qt/KDE Application and how to fix
This should be reported and fixed upstream; good catch!
I have just submitted a change to Gerrit about this in here.