Results 1 to 17 of 17

Thread: Dynamical objects (by operator new) in Qt

  1. #1
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    3

    Default Dynamical objects (by operator new) in Qt

    How to delete dynamic objects (creating by operator new)? Freed memory by the operating system or some Qt-means? Thanks.

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

    Default Re: Dynamical objects (by operator new) in Qt

    Use the "delete" operator.
    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
    Oct 2011
    Posts
    8
    Thanks
    3

    Default Re: Dynamical objects (by operator new) in Qt

    Quote Originally Posted by wysota View Post
    Use the "delete" operator.
    Yes, I know But in Examples and Demos uses no one nowhere operator delete in destructors. I asked another question

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,346
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Dynamical objects (by operator new) in Qt

    In general, any object instance that is created by operator new() and is given to another object instance as its child does not have to be deleted. Qt QObject parent-child relationships will ensure that when an instance's parent gets deleted, all of its children will be deleted, too.

    Since most apps create QApplication and QMainWindow instances on the stack, in main(), when the program exits these instances and all of their heap-created children will be automatically cleaned up.

    One exception to this is the use of heap-allocated QAction instances. One instance of a QAction can be shared with multiple object instances (such as assigning the same QAction instance to a menu, push-button, and toolbar button). These should be made member variables of some higher-level GUI class (such as your QMainWindow) and should be deleted in the destructor of that class. Assigning a QAction to a menu item does not establish a parent-child relationship.
    Last edited by d_stranz; 10th October 2011 at 17:41.

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

    Default Re: Dynamical objects (by operator new) in Qt

    Quote Originally Posted by spherrra View Post
    I asked another question
    I don't see any other questions:
    How to delete dynamic objects (creating by operator new)? Freed memory by the operating system or some Qt-means? Thanks.
    You asked how to delete objects created using "new" operator. That's what you use the "delete" operator for.

    It really pays off to invest some time in learning to write proper English sentences.


    One exception to this is the use of heap-allocated QAction instances. One instance of a QAction can be shared with multiple object instances (such as assigning the same QAction instance to a menu, push-button, and toolbar button). These should be made member variables of some higher-level GUI class (such as your QMainWindow) and should be deleted in the destructor of that class.
    That's not true. I'd like to see you creating a QAction object as a member variable... QAction is by any means similar to other QObject subclasses, if you delete a parent of QAction, the action gets deleted too. The only difference is that QAction requires a parent (thus it makes no sense to make it a member variable of another QObject).
    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.


  6. #6
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    3

    Default Re: Dynamical objects (by operator new) in Qt

    In Qt Ex. and Demos objects-parent (of the first level) are dynamical objects and they are not removed by "delete" in destructors. Why?
    Last edited by spherrra; 10th October 2011 at 17:55.

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,346
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Dynamical objects (by operator new) in Qt

    Quote Originally Posted by wysota View Post
    That's not true. I'd like to see you creating a QAction object as a member variable... QAction is by any means similar to other QObject subclasses, if you delete a parent of QAction, the action gets deleted too. The only difference is that QAction requires a parent (thus it makes no sense to make it a member variable of another QObject).
    OK, you are probably right. In my MainWindow classes, I do create actions as children of this. But why does deleting them in the MainWindow destructor not cause a memory fault? If the action instances were in fact first-class children of the MainWindow instance, shouldn't they have already been deleted by the QObject system before it gets to my destructor? (Or if destruction occurs in the reverse order, shouldn't my deletes cause a fault somewhere further up the line as destructors for MainWindow's superclasses get invoked?)

    And by "member variable" I meant that the member variable is a QAction *, not QAction.

    If the QObject system guarantees that QAction instances are also deleted when their parent goes out of scope, that would save me a lot of otherwise useless member variables.

    In Qt Ex. and Demos objects-parent (of the first level) are dynamical objects and they are not removed by "delete" in destructors. Why?
    In most of the demos that I have looked at, the base object of the GUI is derived from QMainWindow or QDialog, and is allocated on the stack in the main() procedure. Any child that is created further down in the code is created as a child of this main window, and is therefore owned and and has its lifetime controlled by it.
    Last edited by d_stranz; 10th October 2011 at 18:05.

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

    Default Re: Dynamical objects (by operator new) in Qt

    Quote Originally Posted by d_stranz View Post
    I do create actions as children of this.
    Children and members are two distinct things.
    But why does deleting them in the MainWindow destructor not cause a memory fault?
    Why would it?

    If the action instances were in fact first-class children of the MainWindow instance, shouldn't they have already been deleted by the QObject system before it gets to my destructor?
    Think what is the order of destructor calls when using C++ inheritance.

    And by "member variable" I meant that the member variable is a QAction *, not QAction.
    Here a pointer to an object is a member variable, not the object itself. You can have 10 pointers pointing to the same object and all these can be member variables of different objects. The pointed object doesn't have to be a member variable of any object.
    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
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,346
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Dynamical objects (by operator new) in Qt

    Pardon me for using imprecise terminology. I have a bit more than 20 years of C++ experience, and I understand the differences between heap and stack allocated objects, and how pointers and references work. I do remember the order in which destructors are called but if you write semantically correct code it doesn't usually matter.

    And IMO, a "member variable" can be a value that is stack-allocated during construction, a reference assigned during construction, or a pointer to a heap-allocated instance created and assigned at any time during the lifetime of an object instance. You imply that it is only the first type. If so, then what do you call a pointer variable that is defined as a member of a class if not a "member variable"?

    Nomenclature aside, if I create a pointer to a QObject type using another QObject instance as its parent, store that pointer in a pointer member variable of that class, then delete that pointer in my class destructor, why doesn't that cause a memory fault? Creating that pointer and setting my class instance as its parent presumably stored it away somewhere in Qt's object hierarchy system with a flag that says, "when this parent goes out of scope, delete this child, too". So, if I delete it first, then that leaves a dangling pointer which should cause a memory fault when Qt tries to delete it later. Why not? Is the QObject constructor written in such a way that deleting a QObject instance also removes it from any parent-child relationship it might be in?

    If this is true, then is there ever a case in Qt where a call to delete is required, except at the very top of a heap-allocated object hierarchy?

  10. #10
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    3

    Default Re: Dynamical objects (by operator new) in Qt

    For example: Qt 4.7 Examles and Demos, OpenGL, Hello GL Example

    There are two classes GLWidget and Window. I cite the file window.cpp
    Qt Code:
    1. Window::Window()
    2. {
    3. glWidget = new GLWidget;
    To copy to clipboard, switch view to plain text mode 
    The object "glWidget" is dynamical. But it is not the object-child of object of the Window. I must so writte
    Qt Code:
    1. Window::Window()
    2. {
    3. glWidget = new GLWidget(this);
    To copy to clipboard, switch view to plain text mode 

    or writte destructor
    Qt Code:
    1. Window::~Window()
    2. {
    3. delete glWidget;
    To copy to clipboard, switch view to plain text mode 
    But it's not in this example. Why?

  11. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,346
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Dynamical objects (by operator new) in Qt

    Quote Originally Posted by spherrra View Post
    For example: Qt 4.7 Examles and Demos, OpenGL, Hello GL Example

    But it's not in this example. Why?
    Look at these lines:

    Qt Code:
    1. QHBoxLayout *mainLayout = new QHBoxLayout;
    2. mainLayout->addWidget(glWidget);
    3.  
    4. // ...
    5.  
    6. setLayout(mainLayout);
    To copy to clipboard, switch view to plain text mode 

    This first line creates a QHBoxLayout. The second line assigns it as the parent of glWidget. The third line assigns Window as the parent of mainLayout. So, mainLayout is owned by Window, and glWidget is owned by mainLayout. Each of these children will be deleted automatically when Window goes out of scope.

  12. The following user says thank you to d_stranz for this useful post:

    spherrra (10th October 2011)

  13. #12
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    3

    Default Re: Dynamical objects (by operator new) in Qt

    Ok. Thank you! I understood this. Спасибо

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Dynamical objects (by operator new) in Qt

    Quote Originally Posted by d_stranz View Post
    And IMO, a "member variable" can be a value that is stack-allocated during construction, a reference assigned during construction, or a pointer to a heap-allocated instance created and assigned at any time during the lifetime of an object instance. You imply that it is only the first type. If so, then what do you call a pointer variable that is defined as a member of a class if not a "member variable"?
    I didn't say anything like that. I said the pointer is a member variable and not the object it points to. If you have "int *x" then the variable which is a memeber of the object that contains it is "x" and not "*x".

    Nomenclature aside, if I create a pointer to a QObject type using another QObject instance as its parent, store that pointer in a pointer member variable of that class, then delete that pointer in my class destructor, why doesn't that cause a memory fault?
    Because QObject destructor is called after your destructor and the object you delete has time to detach itself from its parent and thus is not deleted again when the parent is deleted. The code is equivalent to:

    Qt Code:
    1. QObject::~QObject() { qDeleteAll(children); setParent(0); }
    2. void QObject::setParent(QObject *newParent) {
    3. if(parent()) parent()->children().remove(this); // of course ignoring that children() returns a copy
    4. m_parent = newParent;
    5. if(m_parent) m_parent->children().append(this); // of course ignoring that children() returns a copy
    6. }
    To copy to clipboard, switch view to plain text mode 

    If this is true, then is there ever a case in Qt where a call to delete is required, except at the very top of a heap-allocated object hierarchy?
    A call to delete is only required if a QObject-derived (and heap allocated) object has no parent (and is not assigned to some kind of smart pointer). I usually have 0 QObject-related "delete" calls in my programs

    Oh... one more thing. Never create QObjects with a parent on the stack (unless they are modal QDialogs which is usually safe). Then you can have double deletions (the behaviour is compiler-dependent).


    Added after 8 minutes:


    Quote Originally Posted by d_stranz View Post
    This first line creates a QHBoxLayout. The second line assigns it as the parent of glWidget. The third line assigns Window as the parent of mainLayout. So, mainLayout is owned by Window, and glWidget is owned by mainLayout. Each of these children will be deleted automatically when Window goes out of scope.
    To be exact:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char **argv){
    4. QApplication app(argc, argv);
    5. QWidget *w = new QWidget;
    6. l->addWidget(w);
    7. qDebug() << ( w->parent() ? w->parent()->metaObject()->className() : "no parent");
    8. QDialog dlg;
    9. dlg.setLayout(l);
    10. qDebug() << ( w->parent() ? w->parent()->metaObject()->className() : "no parent");
    11. qDebug() << ( l->parent() ? l->parent()->metaObject()->className() : "no parent");
    12. return 0;
    13. }
    To copy to clipboard, switch view to plain text mode 

    yields:

    no parent
    QDialog
    QDialog
    The child widget is reparented to the widget and not to the layout.
    Last edited by wysota; 10th October 2011 at 19:33.
    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.


  15. The following user says thank you to wysota for this useful post:

    spherrra (10th October 2011)

  16. #14
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,346
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Dynamical objects (by operator new) in Qt

    Qt Code:
    1. QObject::~QObject() { qDeleteAll(children); setParent(0); }
    2. void QObject::setParent(QObject *newParent) {
    3. if(parent()) parent()->children().remove(this); // of course ignoring that children() returns a copy
    4. m_parent = newParent;
    5. if(m_parent) m_parent->children().append(this); // of course ignoring that children() returns a copy
    6. }
    To copy to clipboard, switch view to plain text mode 
    OK, thanks. This is exactly what I was referring to when I asked if deleting a child removed it from its parent's list of children. So my "extra" deletes do no harm (but don't do much good, either). Away with them!

    The child widget is reparented to the widget and not to the layout.
    Ok, I had known that also, but was trying to keep the explanation more simple.

  17. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Dynamical objects (by operator new) in Qt

    Quote Originally Posted by d_stranz View Post
    Ok, I had known that also, but was trying to keep the explanation more simple.
    Sure, but in two years from now somebody would put the following post right under yours:

    Hi d_stranz, so glad I have found this forum!

    I'm a newbie in Qt and a newbie in C++. I have written the following code:

    Qt Code:
    1. int main(...) {
    2. QWidget *w = new QWidget;
    3. l->addWidget(w);
    4. delete l;
    5. }
    To copy to clipboard, switch view to plain text mode 

    According to what you have said when "l" is deleted, it should delete "w" but it doesn't! I'm using Qt 5.2.7. Is this a bug in Qt? Should I report it to <whoever owns Qt in two years>?
    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.


  18. The following user says thank you to wysota for this useful post:

    d_stranz (10th October 2011)

  19. #16
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,346
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Dynamical objects (by operator new) in Qt

    Point taken.

    <whoever owns Qt in two years>
    That'll be either Google or Microsoft. (or maybe Oracle... no, Ellison couldn't be that greedy).

  20. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Dynamical objects (by operator new) in Qt

    Or /dev/null. Certainly not Oracle.
    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.


Similar Threads

  1. Replies: 9
    Last Post: 16th March 2010, 10:30
  2. How to make layout dynamical?
    By rambo83 in forum Qt Programming
    Replies: 2
    Last Post: 6th January 2010, 16:05
  3. Dynamical LineEdit and signals
    By martisho in forum Qt Programming
    Replies: 5
    Last Post: 10th November 2009, 16:39
  4. Replies: 8
    Last Post: 8th April 2009, 20:51
  5. Simple: Operator overloading with heap objects
    By durbrak in forum General Programming
    Replies: 12
    Last Post: 25th April 2007, 14:20

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
  •  
Qt is a trademark of The Qt Company.