Results 1 to 11 of 11

Thread: Memory management in Qt programs

  1. #1
    Join Date
    Jan 2016
    Posts
    60
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Memory management in Qt programs

    Hi all,

    I've got a Qt project which the statement below is written in its .cpp file's constructor:

    Qt Code:
    1. setAttribute(Qt::WA_DeleteOnClose);
    To copy to clipboard, switch view to plain text mode 

    The program, by the way, inherits from QMainWindow:

    in the header file:
    Qt Code:
    1. class MyProgram : public QMainWindow
    To copy to clipboard, switch view to plain text mode 

    I think the meaning of the first statement is that, when I close the project (MyProgram), the Qt Creator compiler (MinGW) deletes all objects created by new in the project.
    Is it right up to here?

    If so, why is it needed whatsoever? When we close the project, on Windows/Mac etc, all objects, statements, functions and whatever exists inside the project will be freed/deleted and when we open the project once again, they will be assigned once again. So, I don't know why we still need:
    Qt Code:
    1. setAttribute(Qt::WA_DeleteOnClose);
    To copy to clipboard, switch view to plain text mode 

    To me, it does nothing and is redundant.

    Am I wrong, please?
    If possible please guide me in this case.

  2. #2
    Join Date
    Jul 2008
    Posts
    397
    Thanks
    9
    Thanked 51 Times in 49 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Memory management in Qt programs

    Hi, WA_DeleteOnClose means that the widget where it is set will be deleted when the widget is closed (see https://doc.qt.io/qt-5/qt.html#WidgetAttribute-enum). This will not delete all objects created by new (only those whose parent is the widget with WA_DeleteOnClose set).

    Ginsengelf

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,323
    Thanks
    36
    Thanked 1,526 Times in 1,477 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Memory management in Qt programs

    Quote Originally Posted by franky View Post
    If so, why is it needed whatsoever?
    It is not needed, it is convenient.

    For example with a sub window that is created on demand, deleted when it is no longer needed and re-created on demand again.
    E.g. a Find/Replace dialog, a log/debug view window, etc.

    Quote Originally Posted by franky View Post
    When we close the project, on Windows/Mac etc
    Ending a program is something entirely different.

    Explicitly deleting/freeing resources during runtime is about controlling the resource usage of the program while it is operational.

    Obviously no resources are needed anymore when the process stops existing.
    But even then it is still a good idea to explicitly release resources as this allows control over order of release and to run code during release.

    Cheers,
    _

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

    franky (9th April 2019)

  5. #4
    Join Date
    Jan 2016
    Posts
    60
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Memory management in Qt programs

    So it's for a sub window. When we close it, its resources will be free.

    > But even then it is still a good idea to explicitly release resources as this allows control over order of release and to run code during release.

    I can't understand it completely, may you explain it more or using an example?

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,323
    Thanks
    36
    Thanked 1,526 Times in 1,477 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Memory management in Qt programs

    Quote Originally Posted by franky View Post
    So it's for a sub window. When we close it, its resources will be free.
    It works on any kind of widget. A sub window is one of the use case.

    Quote Originally Posted by franky View Post
    > But even then it is still a good idea to explicitly release resources as this allows control over order of release and to run code during release.

    I can't understand it completely, may you explain it more or using an example?
    For example if your program has an open network connection, then explicitly closing it will notify the other side.
    If the program just terminates then the other side will run into an error or timeout, potentially much much later then if it had been notified about the connection being closed.

    Cheers,
    _

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

    franky (9th April 2019)

  8. #6
    Join Date
    Jan 2016
    Posts
    60
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Memory management in Qt programs

    Thank you.

    By "explicitly closing it" do you mean using the "delete" keyword or the functions which are built to perform this task like "setAttribute(Qt::WA_DeleteOnClose);"?

    And should we do this (explicitly closing) for any kinds of resources whether they're widgets, containers or something else that are created using "new"? I mean, when we dedicate something using the keyword "new" to something else we must somewhere in the code free that thing either using "delete" or a function like the above one. Right?

  9. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,323
    Thanks
    36
    Thanked 1,526 Times in 1,477 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Memory management in Qt programs

    Quote Originally Posted by franky View Post
    By "explicitly closing it" do you mean using the "delete" keyword or the functions which are built to perform this task like "setAttribute(Qt::WA_DeleteOnClose);"?
    Yes. In the case of the socket, potentially even calling close() or disconnectFromHost(), but I think the destructor does that as well.

    Quote Originally Posted by franky View Post
    And should we do this (explicitly closing) for any kinds of resources whether they're widgets, containers or something else that are created using "new"? I mean, when we dedicate something using the keyword "new" to something else we must somewhere in the code free that thing either using "delete" or a function like the above one. Right?
    Right.

    Or using smart pointers for resources allocated with "new" and which are not widgets with DeleteOnClose or in general QObjects with a parent.

    The rule of thumb is: if it has been created with "new" it must be destroyed with "delete", unless ownership of the pointer is transferred to a different mechansim.
    Such mechanisms can be smart pointers, QObject parent/child relationship, or things like DeleteOnClose for widgets.

    Cheers,
    _

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

    franky (9th April 2019)

  11. #8
    Join Date
    Jan 2016
    Posts
    60
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Memory management in Qt programs

    Thanks so much, I got many things.

    One thing:
    > QObject parent/child relationship

    Does it mean that we mustn't delete such a thing? Or do you mean that in such a relationship we only need to delete the parent and the children are freed afterwards?

    So in this mechanism too, we need deletion, not?

  12. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,323
    Thanks
    36
    Thanked 1,526 Times in 1,477 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Memory management in Qt programs

    Quote Originally Posted by franky View Post
    > QObject parent/child relationship

    Does it mean that we mustn't delete such a thing? Or do you mean that in such a relationship we only need to delete the parent and the children are freed afterwards?
    Yes, correct. In a QObject tree, a node will delete its children when it itself is being deleted.

    Quote Originally Posted by franky View Post
    So in this mechanism too, we need deletion, not?
    Well, the root node of a QObject tree will need to be deleted if it has been created with "new".
    This deletion can be explicit or use any of the other techniques mentioned earlier

    Cheers,
    _

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

    franky (10th April 2019)

  14. #10
    Join Date
    Jan 2016
    Posts
    60
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Memory management in Qt programs

    Thanks so much.

    One thing about resources built using methods apart from "new". For instance, a class embracing a number of data members and member functions in a C++/Qt/QML project. At times in such projects I still see a destructor while there's not any keyword "new" in the code. So for such projects/codes since there is no memory allocated/assigned using "new", we mustn't make the code more complicated by declaring and defining destructors or other mechanisms to delete the objects or generally the resources in the code. Do you agree?

  15. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,323
    Thanks
    36
    Thanked 1,526 Times in 1,477 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Memory management in Qt programs

    The destructor is the code that is run when an object is destroyed.

    It can be used to call delete on pointer members of that class but of course it can do other things as well, e.g. calling close() on a socket.

    It will run independent of how the object itself is being destroyed, e.g. deleted after new or simply falling out of scope when allocated on the stack.

    Sometimes classes have an empty constructor to enable forward declaration of certain members, e.g. the template values of smart pointers.

    Essentially the same rules as for constructors: if the class doesn't need any special initialization, then the constructor generated by the compiler will be good enough.
    If the class doesn't need any special deinitialization, then the destructor generated by the compiler will be enough.

    Rule of thumb: if you have pointer based members you likely want both.

    Cheers,
    _

Similar Threads

  1. Memory management problem
    By zloigall in forum Newbie
    Replies: 4
    Last Post: 25th July 2012, 16:16
  2. Memory management in Qt?
    By wookoon in forum Qt Programming
    Replies: 7
    Last Post: 6th November 2010, 18:20
  3. Memory Management Reg
    By BalaQT in forum Newbie
    Replies: 10
    Last Post: 4th February 2010, 12:43
  4. Memory management
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2008, 20:48
  5. Memory management in QT
    By Gayathri in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2006, 07:21

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.