PDA

View Full Version : Memory Managment Question



tntcoda
25th June 2008, 16:22
Hi,

If i have this code to start up a dialog from my main window:



GoQueueInfoDlg *dlg = new GoQueueInfoDlg(this, qu);
dlg->show();


Do i have to delete dlg (Subclass of QDialog) at some point? I wasn't sure if qt auto deletes it because its not a child of something :confused:

If i do need to delete it, where do i do it? I could do it in the destructor of my main window, but is there any point if the program terminates there anyway?

Thanks,
Jack

JimDaniel
25th June 2008, 16:29
if the "this" you're passing into the constructor is the main window, then your dialog's parent is the main window and it will destroy the object, AFAIK. If you needed to delete it, you'd want to do it in the destructor for the object where its instantiated.

tntcoda
25th June 2008, 16:38
Brilliant, thanks very much.

jpn
25th June 2008, 16:52
Since the dialog is allocated on the heap, you may also use attributes to make the dialog automatically deleted once it's closed:


dlg->setAttribute(Qt::WA_DeleteOnClose);

wysota
25th June 2008, 17:34
An immediate conclusion from what J-P said is that if you close a dialog, it will not get deleted by itself by default. This will happen only if its parent is destroyed or if you pass the delete on close attribute and close the window.