Results 1 to 14 of 14

Thread: Deleting QMainWindow and QApplication Objects

  1. #1
    Join Date
    Apr 2013
    Posts
    63
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Deleting QMainWindow and QApplication Objects

    I understand that the QWidget will delete its child widgets appropriately. What about the objects of QMainwindow, the super parent and QApplication. Do I need to delete them explicitly to clear the memory?

    If it is an executable, (i.e. the QMainWindow and QApplication objects are created in main function), it may be fine because the entire application is going to exit anyway.

    But what happens in case of a dll (i.e. QMainWindow and QApplication objects are created inside an exported dll-function)?
    In this case, The dll-function is invoked from a parent application and later, QMainWindow is closed by the user. Remember that the parent application is still running. When the same session of parent application invokes the dll-function again, the Qt objects are created newly. So is there any chance of memory issues when it happens multiple time?

  2. #2
    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: Deleting QMainWindow and QApplication Objects

    Quote Originally Posted by mqt View Post
    I understand that the QWidget will delete its child widgets appropriately. What about the objects of QMainwindow, the super parent and QApplication. Do I need to delete them explicitly to clear the memory?
    The application object is usually created on the stack in main() so there is no need to delete it. As for main windows, they are widgets as any other so the same rules apply to it as to any other widget.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2013
    Posts
    63
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Deleting QMainWindow and QApplication Objects

    Thanks for the reply,

    So who deletes the topmost parent widget?
    And what about the second part of question? In case of DLL, they are not created on the stack in main().

  4. #4
    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: Deleting QMainWindow and QApplication Objects

    Quote Originally Posted by mqt View Post
    So who deletes the topmost parent widget?
    If it was allocated on stack then the compiler, if it was allocated on heap then the programmer

    And what about the second part of question? In case of DLL, they are not created on the stack in main().
    I don't know where and how you create the application object therefore I cannot answer your question. In general regular C++ rules apply here.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Apr 2013
    Posts
    63
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Deleting QMainWindow and QApplication Objects

    OK,
    My only concern was this: if Qt also deletes these objects, it will be memory corruption. I know Qt deletes the memory for widgets. Just wanted to know if it deletes topmost parent widget and QApplication objects also. If Qt wont delete I can delete them. But I wanted to be sure that it is not double deleted. Thanks for the reply

  6. #6
    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: Deleting QMainWindow and QApplication Objects

    There are two rules:

    1. When an object is deleted, it deletes all its children
    2. When object scope ends, it is deleted by the compiler

    So there is no rule that "Qt deletes memory for widgets" or that it doesn't delete memory for the application object. All depends on the object scope and the scope of its parent (if present).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Apr 2013
    Posts
    63
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Deleting QMainWindow and QApplication Objects

    Those rules are for stack, right?
    I am talking about the memory on heap which will not be deleted automatically when scope ends (QMainWindow *window = new QMainWIndow() ).
    I was thinking, even for memory on heap, Qt deletes QWidget objects when its parent is closed.

  8. #8
    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: Deleting QMainWindow and QApplication Objects

    Quote Originally Posted by mqt View Post
    Those rules are for stack, right?
    No, those rules are for everything.

    I am talking about the memory on heap which will not be deleted automatically when scope ends (QMainWindow *window = new QMainWIndow() ).
    The scope of the pointer ends, not the object behind it. The pointer will be deleted, the object will not.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Deleting QMainWindow and QApplication Objects

    Quote Originally Posted by wysota View Post
    No, those rules are for everything.


    The scope of the pointer ends, not the object behind it. The pointer will be deleted, the object will not.
    Unless you use QScopedPointer.

  10. #10
    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: Deleting QMainWindow and QApplication Objects

    Quote Originally Posted by Lesiok View Post
    Unless you use QScopedPointer.
    A scoped pointer is an object, not a pointer
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Apr 2013
    Posts
    63
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Deleting QMainWindow and QApplication Objects

    Thanks for all your comments. But still my doubt is not cleared.

    I am clear about following
    - In stack, the objects will get deleted when the scope ends
    - In heap, we need to explicitly call "delete" or free to "clear" the memory
    - When you declare a pointer, the pointer itself is in stack but the corresponding data is allocated in heap when we use "new" or "malloc"
    - All above things are c/c++ concepts and nothing special about Qt
    - In Qt, When we use QMainWindow *win=new QMainWindow(), the "win" is in stack and actual data pointed by "win" is in heap.
    - If I create and add a widget to "win" by QWidget *childWin = new QWidget(win); actual data pointed by childWin is in heap. But I dont need to "delete" that explicitly because when "win" destructor is called, the "childWin" will be deleted. This is a Qt functionality.

    But I am still not clear about following.
    - As there is no parent for "win" (and QApplication *app), do I need to call "delete win" and "delete app" explicitly.

    I am facing some memory corruption during the exit of my code. I am suspecting that I am doing double delete somewhere. Understanding what Qt does will help.

  12. #12
    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: Deleting QMainWindow and QApplication Objects

    Quote Originally Posted by mqt View Post
    But I am still not clear about following.
    - As there is no parent for "win" (and QApplication *app), do I need to call "delete win" and "delete app" explicitly.
    The object is on a heap and has no parent to delete it. The conclusion seems clear.
    I am facing some memory corruption during the exit of my code. I am suspecting that I am doing double delete somewhere. Understanding what Qt does will help.
    Use Valgrind.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Deleting QMainWindow and QApplication Objects

    Quote Originally Posted by wysota View Post
    A scoped pointer is an object, not a pointer
    Of course, but here we can say "it is a better pointer".

  14. #14
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Deleting QMainWindow and QApplication Objects

    Quote Originally Posted by mqt View Post
    - As there is no parent for "win" (and QApplication *app), do I need to call "delete win" and "delete app" explicitly.
    For widgets you can, as an additional option, set the Qt::WA_DeleteOnClose attribute. Which delete the widget on close.

    Cheers,
    _

Similar Threads

  1. Deleting objects in their event handler
    By drhex in forum Qt Programming
    Replies: 7
    Last Post: 6th May 2009, 16:08
  2. creating/deleting Objects // memory leaks
    By janus in forum Qt Programming
    Replies: 4
    Last Post: 27th March 2008, 18:17
  3. Q_FOREACH deleting objects
    By veda in forum Qt Programming
    Replies: 31
    Last Post: 22nd May 2007, 13:05
  4. Reponsabilities of QApplication and QMainWindow classes
    By yellowmat in forum Qt Programming
    Replies: 4
    Last Post: 4th September 2006, 16:21
  5. Replies: 7
    Last Post: 18th July 2006, 21:33

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.