Results 1 to 8 of 8

Thread: main.cpp and creating an instance of QMainWindow

  1. #1
    Join Date
    Jun 2015
    Location
    California, USA
    Posts
    61
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default main.cpp and creating an instance of QMainWindow

    I've noticed that all the examples in the Qt documentation
    create the toplevel instance of QMainWindow using the stack.
    The project I'm working on creates it (in the main function) using the heap.
    Regarding the missing call to delete, does it matter in this case?
    Is everything still properly deleted because the program terminates?

  2. #2
    Join Date
    Jan 2011
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: main.cpp and creating an instance of QMainWindow

    you mean that you create a instance of QMainWindow by "new ****"?
    i thought you'd better delete each pointer for freeing res.

  3. The following user says thank you to weiweiqiao for this useful post:

    ravas (23rd October 2015)

  4. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: main.cpp and creating an instance of QMainWindow

    Quote Originally Posted by ravas View Post
    Is everything still properly deleted because the program terminates?
    No, the destructor is not called. The OS typically frees all the resources it tracks, such as memory and open file descriptors, but that's all. If, for instance, the destructor of your main window saves some data to a configuration file, you need the destructor to be called, e.g. with delete.

    More generally, it is good practice to explicitly release the resources you allocate. If you want to allocate the main window on the heap and still have it automatically deleted at the end of the enclosing scope, consider wrapping the pointer in a smart pointer, such as std::unique_ptr.

  5. The following user says thank you to yeye_olive for this useful post:

    ravas (23rd October 2015)

  6. #4
    Join Date
    Jun 2015
    Location
    California, USA
    Posts
    61
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: main.cpp and creating an instance of QMainWindow

    Thanks guys.

  7. #5
    Join Date
    Jun 2015
    Location
    California, USA
    Posts
    61
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: main.cpp and creating an instance of QMainWindow

    It turns out the project was using
    Qt Code:
    1. setAttribute(Qt::WA_DeleteOnClose);
    To copy to clipboard, switch view to plain text mode 

    It seems like this feature is intended for windows other than the top level QMainWindow.

  8. #6
    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: main.cpp and creating an instance of QMainWindow

    Quote Originally Posted by ravas View Post
    It seems like this feature is intended for windows other than the top level QMainWindow.
    It can be used for any widget that is not allocated on the stack.

    Usually used for widgets that are windows, e.g. QMainWindow, QDialog based windows, or other widgets without parent.

    Cheers,
    _

  9. The following user says thank you to anda_skoa for this useful post:

    ravas (24th October 2015)

  10. #7
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Thanks
    33
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: main.cpp and creating an instance of QMainWindow

    Quote Originally Posted by yeye_olive View Post
    No, the destructor is not called. The OS typically frees all the resources it tracks, such as memory and open file descriptors, but that's all. If, for instance, the destructor of your main window saves some data to a configuration file, you need the destructor to be called, e.g. with delete.

    More generally, it is good practice to explicitly release the resources you allocate. If you want to allocate the main window on the heap and still have it automatically deleted at the end of the enclosing scope, consider wrapping the pointer in a smart pointer, such as std::unique_ptr.
    If the OS, frees it, then why do we have memory leaks ? why do we even care to free the memory in first place?
    (Is it that OS will not reclaim memory as long as the app is running, and thus if the app doesn't free it, while working, it may very soon consume all memory because of that leakage!?)

  11. The following user says thank you to Hossein for this useful post:

    ravas (24th October 2015)

  12. #8
    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: main.cpp and creating an instance of QMainWindow

    Quote Originally Posted by Hossein View Post
    If the OS, frees it, then why do we have memory leaks ?
    As yeye_olive already wrote, memory is not the problem.

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 5th July 2013, 15:09
  2. Replies: 5
    Last Post: 9th September 2011, 04:02
  3. Replies: 0
    Last Post: 2nd May 2010, 03:55
  4. Replies: 2
    Last Post: 4th February 2010, 07:54
  5. creating a global instance
    By skatakam in forum General Programming
    Replies: 6
    Last Post: 24th February 2006, 17:26

Tags for this Thread

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.