Memory Management in Qt: Address Book Tutorial

So, so far, I have learned that Qt frees the memory of all child objects when the parent object is deleted. In addition, there is usually no need to worry about memory management for objects created on the stack (i.e. NOT as pointers).

Now that I have done a very good “AddressBook” tutorial , I found this in Part 5:

AddressBook::AddressBook(QWidget *parent) : QWidget(parent)
{
    dialog = new FindDialog;
}

Full source available: addressbook.h addressbook.cpp finddialog.h

Here dialogis a private member AddressBook, and he is a pointer to FindDialog. FindDialoginherits QDialog, but no this-Pointer is passed to the constructor (as shown above). There is no explicit destructor, no delete dialog-call ...

Also, non-passing thisseems intentional:

[FindDialog constructor] is defined to accept the parent QWidget, although the dialog will open as a separate window.

Wouldn't that lead to a memory leak? Or is there some other mechanism that silently removes dialogand frees his memory?

Thanks in advance for your help!

Update: . I posted this issue on the qt-project.org forums and it should be fixed soon.

+4
source share
1 answer

There is no excuse for this, and as a result, he has more problems than you just raise, namely:

  • It is not controlled, as you say.

  • It does not use the usual new Foo () syntax.

  • This is not done on the constructor initializer list.

OS, , , , , , . , -, - QPointer.

. :

() Qt/KDE

; !

Gerrit .

+3

Source: https://habr.com/ru/post/1537352/


All Articles