Results 1 to 5 of 5

Thread: Memory Leaks

  1. #1
    Join Date
    Jun 2008
    Posts
    89
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Memory Leaks

    Hi,

    Say in a dialog i have a menu and i have added some actions to the menu
    Qt Code:
    1. Qmenu *menu = new QMenu();
    2. QAction *addGroupAction = new QAction("ADD", this);
    3. QAction *renameGroupAction = new QAction("RENAME", this);
    4. menu->addAction(addGroupAction);
    5. menu->addAction(renameGroupAction);
    To copy to clipboard, switch view to plain text mode 

    No the question is
    Whether the actions get deleted automatically when the menu is deleted.
    and whether the menu is destroyed automatically when dialog is destoyed..


    I mean have QT provided any such automatic garbage collection. or i have to do it explicitly by calling delete function.

    Please help...

    GK

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Memory Leaks

    May be this will answer your question

  3. #3
    Join Date
    Jun 2008
    Posts
    89
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Memory Leaks

    That is case of Parent-Child relationships...
    but i am creation the QMenu instance on heap in the constructor of the dialog.
    so does this mean when i close the dialog the menu object is destroyed...
    and consequencty QAction objects are destroyed.
    ????

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Memory Leaks

    We don't know what you are doing with the menu. So we can not say what will happen.
    i) If you create any object with new but do not set (or pass) a parent, it will usually not get deleted.
    ii) It might get deleted, because you might have connected a signal to the QObject::deleteLater() slot, or a QWidget might have the Qt::WA_DeleteOnClose flag set.
    iii) Why don't you create your popup menu on the stack?
    Qt Code:
    1. QMenu menu;
    2. menu.addAction(...);
    3. ...
    4. QAction * sel = menu.exec();
    To copy to clipboard, switch view to plain text mode 
    This is faster and avoid any memory management issues.

    HTH

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

    Default Re: Memory Leaks

    Quote Originally Posted by kaushal_gaurav View Post
    That is case of Parent-Child relationships...
    but i am creation the QMenu instance on heap in the constructor of the dialog.
    so does this mean when i close the dialog the menu object is destroyed...
    and consequencty QAction objects are destroyed.
    ????
    You have basically three choices:
    1) Take a look at Qt sources to see how addAction is implemented
    2) Connect a slot to signal destroyed() of each action and see whether it fires when you destroy (destroy, not close) the menu
    3) Think what would happen in this situation and which solution is more logical:
    Qt Code:
    1. QAction *action = new QAction(...);
    2. toolBar->addAction(action);
    3. menu->addAction(action);
    4. delete toolBar;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Memory leaks..
    By santhoshv84 in forum Qt Programming
    Replies: 2
    Last Post: 28th August 2008, 19:28
  2. Memory leaks in QT apps
    By sar_van81 in forum Qt Programming
    Replies: 7
    Last Post: 19th July 2008, 08:32
  3. Memory leaks when read/write to DOM
    By SailinShoes in forum Qt Programming
    Replies: 6
    Last Post: 20th March 2008, 11:51
  4. memory leaks
    By Fastman in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2008, 08:00
  5. why there are memory leaks in Qt?
    By cocalele in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2006, 09:55

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.