[SOLVED] [N00b] Memory management in Qt
-
Basiclly, if your instance has parent object(which is QObject base), it will be deleted when parent be deleted. So, firstly, make sure if it's QObject-based. If the answer is not, you can delete the pointer in the deconstructor. Or use smart pointer in Qt.
-
If you use QML with Qt C++, it's more complicated. But the core concept you need know is ownership. Also in QtScript. You can also think about if the pointer be deleted twice, and what will happen in Qt.
-
Okay.
For instance, when I close a dialog using, say
@connect(EMP_closeButton, SIGNAL(clicked()), this, SLOT(close()));@
will this call the destructor, or will it just hide the dialog?
Do I need to set Qt::WA_DeleteOnClose? If yes, where do I do that?
Thanks for your incredibly fast reponses, guys!
-
Yes, you should give your QSqlQueryModel instances an explicit parent. If you do not specify a parent, and none is set automatically (for instance inserting a widget into a layout of a parent widget will take care of parent-child relationships automatically), you need to take care of deleting any object you created on the heap yourself. That probably come down to calling delete on the pointers in the destructor of the class you created them in. Or, as Chuck.Gao suggests, use smart pointers to take care of that for you.
-
Okay. So since everything (dialogs, widgets...) I call from the MainWindow has the MainWindow as its parent, the delete process is taken care of when I close the window using the code above. Where do I set Qt::DeleteOnClose?
What happens if I delete a pointer twice?
Thanks,
-
close() won't delete the dialog (= call the destructor) unless the WA_DeleteOnClose attribute is set. The flag can be set any time (preferebly before closing) using QWidget::setAttribute, and - of course - if your dialog is created on the heap (if it is created on the stack it will be destructed automatically when going out of scope).
However, if you set a parent for your dialog, it will be automatically deleted as soon as the parent is deleted. If you do not set a parent, it is your responsibility to delete the dialog (by either calling delete or setting the WA_DeleteOnClose attribute).
-
Thanks for answers, Qt devs! I really appreciate it.
-
[quote author="Joey Dumont" date="1309184687"]What happens if I delete a pointer twice? [/quote]
If the pointer has been set to 0 after the first deletion usually nothing, as @
delete 0;
@ is perfectly valid C++ if I remember correctly.However, if you call delete on a non-null pointer which has already been deleted this usually leads to unexpected behaviour and will crash your application at best.
-
In that case, I'll be careful to always declare a parent. Seems like the easiest solution.
Thanks again!