Results 1 to 4 of 4

Thread: QT memory managment

  1. #1
    Join Date
    Nov 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QT memory managment

    Consider the following snippet code:

    Qt Code:
    1. 1: QPushButton *p_Button = new QPushButton(this);
    2. 2: QPushButton myButton(this);
    To copy to clipboard, switch view to plain text mode 

    Line 1: this is referred to QWidget, so p_Button is child of QWidget in my example: when QWidget dies (goes out the scope??) his destructor deletes p_Button from the heap and calls the destructor of p_Button.

    Line 2: Same as Line 1, but does the destructor of QWidget delete myButton since its child is also myButton?

    Please correct me if I stated something wrong and reply to my questions.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QT memory managment

    In line with standard C++ rules myButton is deleted when it goes out of scope. MyButton ceases to exist and is destroyed before its parent can be destroyed; this will remove it from the child list of its Qt parent. When the parent is finally destroyed MyWidget no longer exists as a child to delete.

  3. #3
    Join Date
    Nov 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT memory managment

    Could I delete p_Button manually before QWidget delete it? I'd probably need to delete it when I won't need it anymore and to not cause memory leaks?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QT memory managment

    Yes... and you can try this for yourself with a short program. Each QObject emits the QObject::destroyed() signal as it is destroyed, and the QObject parent (if there is one) uses this to remove the disappearing QObject from its list of children.

    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. class Object: public QObject {
    5. Q_OBJECT
    6. public:
    7. Object(const QString &s, QObject *p = 0): QObject(p) {
    8. qDebug() << "Creating object" << s << "parent" << p;
    9. setObjectName(s);
    10. }
    11. ~Object() {
    12. qDebug() << "Destroying" << objectName() << "with children" << children();
    13. }
    14. };
    15.  
    16. int main(int argc, char **argv)
    17. {
    18. QCoreApplication app(argc, argv);
    19. Object parent("Parent");
    20. Object o1("Stack child", &parent);
    21. Object *h1 = new Object("Heap Child 1", &parent);
    22. Object *h2 = new Object("Heap Child 2", &parent);
    23. delete h1;
    24. qDebug() << "Bye!";
    25. return 0;
    26. }
    27. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Bad memory during endInsertRows
    By Jeffb in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2012, 14:07
  2. Replies: 5
    Last Post: 19th November 2011, 22:05
  3. A way to know the memory use ?
    By tonnot in forum Newbie
    Replies: 4
    Last Post: 23rd February 2011, 09:30
  4. Changing memory allocation managment ( QT 4.4.3 )
    By Link130 in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 20th October 2009, 09:44
  5. Memory Managment Question
    By tntcoda in forum Qt Programming
    Replies: 4
    Last Post: 25th June 2008, 16:34

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.