Results 1 to 5 of 5

Thread: Memory Managment Question

  1. #1
    Join Date
    Jun 2008
    Location
    UK
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Memory Managment Question

    Hi,

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

    Qt Code:
    1. GoQueueInfoDlg *dlg = new GoQueueInfoDlg(this, qu);
    2. dlg->show();
    To copy to clipboard, switch view to plain text mode 

    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

    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

  2. #2
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Memory Managment Question

    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.

  3. #3
    Join Date
    Jun 2008
    Location
    UK
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Memory Managment Question

    Brilliant, thanks very much.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Memory Managment Question

    Since the dialog is allocated on the heap, you may also use attributes to make the dialog automatically deleted once it's closed:
    Qt Code:
    1. dlg->setAttribute(Qt::WA_DeleteOnClose);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Memory Managment Question

    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.

Similar Threads

  1. Memory management
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2008, 20:48
  2. Memory leak weirdness
    By Darhuuk in forum General Programming
    Replies: 10
    Last Post: 10th January 2008, 18:51
  3. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 19:42
  4. Memory question - scribble example program
    By Franco9 in forum Qt Programming
    Replies: 1
    Last Post: 17th October 2006, 03:18
  5. simple pointer question
    By mickey in forum General Programming
    Replies: 6
    Last Post: 16th June 2006, 09:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.