Results 1 to 10 of 10

Thread: About new and delete?

  1. #1
    Join Date
    Jun 2009
    Posts
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default About new and delete?

    First of all ,sorry for my poor English.

    On the internet , I often saw somebody says "You can new a clas without delete it if it inherits from QObject"

    But in the book "C++ GUI Programming with Qt 4 Second Edition", I saw "When we create an object (a widget, validator, or any other kind) with a parent, the parent adds the object to the list of its children. When the parent is deleted, it walks through its list of children and deletes each child. The children themselves then delete all of their children, and so on recursively until none remain."

    So I have a quesstion. Some object such as QString ,it inherits from QObject but have no interface.When I new a QString
    1、I think it has no parent.Am I right?
    2、Need I delete it?

  2. #2
    Join Date
    Jun 2009
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: About new and delete?

    QString is not inherit form QObject.

  3. #3
    Join Date
    Jun 2009
    Posts
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: About new and delete?

    Thanks for your help.I make a mis-example.

    I want to know, When I new a Object( which inherits from QObject but has no parent ), Need I delete it?

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: About new and delete?

    But usually we dont create QString on heap using new, do we ?
    So I dont think there is such need to worry about stack object getting destroyed

  5. #5
    Join Date
    Feb 2009
    Posts
    33
    Thanks
    6
    Thanked 7 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: About new and delete?

    first off, this is really easy to find out on your own -- write your own class derived from QObject and put traces in the destructor to see if it ever gets deleted.

    secondly, "no" -- objects of type QObject do not delete themselves automatically unless they are child objects of a parent object also derived from QObject. Whoever told you this is wrong -- or you misunderstood what they were trying to tell you.

    ---

    an example...

    Qt Code:
    1. QObject *object1 = new QObject;
    2. QObject *object2 = new QObject(object1);
    To copy to clipboard, switch view to plain text mode 

    ...object2 does not need to be deleted explicitly because object1 is its parent -- but object1 does need to be deleted explicitly because it has no parent.

    So when you call "delete" on object1, object2 is also deleted. Conversely, you should NOT call "delete" on object2 unless you remove it from object1's ownership tree.

    OTOH if you did this...

    Qt Code:
    1. QObject *object1 = new QObject;
    2. QObject *object2 = new QObject;
    To copy to clipboard, switch view to plain text mode 
    ...then BOTH of them need to be deleted explicitly since neither one has a parent specified in the constructor.

    ---

    so to be perfectly clear, going back to the first example...

    Qt Code:
    1. QObject *object1 = new QObject;
    2. QObject *object2 = new QObject(object1);
    3.  
    4. [...] // do what you will with object1 and object2...
    5.  
    6. delete object1;
    7.  
    8. [...] // ...don't use object1 and object2 because they're both gone.
    To copy to clipboard, switch view to plain text mode 

    ---

    Since most QObjects (if not all of them) in your apps will be created after the main QApplication object is allocated, you can use the QApplication object as the first parent so you never have orphaned objects derived from QObject...

    another example (given "MyClassFactory" and "MyClass" both derive from QObject)...

    Qt Code:
    1. MyClass *MyClassFactory::createMyClass()
    2. {
    3. return new MyClass(this); // sets myFactory as myClass's parent in "main"...
    4. }
    5.  
    6. int main(int argc, char **argv)
    7. {
    8. // init app...
    9.  
    10. QApplication *myApp = new QApplication(argc, argv);
    11. MyClassFactory *myFactory = new MyClassFactory(myApp);
    12. MyClass *myClass1 = myFactory->createMyClass();
    13. MyClass *myClass2 = myFactory->createMyClass();
    14. // myClass3, myClass4, etc, etc...
    15.  
    16. // run app...
    17.  
    18. app.exec();
    19.  
    20. // cleanup app...
    21.  
    22. /* deleting myApp also deletes myFactory and every object created with MyClassFactory::createMyClass() */
    23.  
    24. delete myApp;
    25.  
    26. //... done
    27.  
    28. return 0;
    29. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 29th June 2009 at 08:10. Reason: missing [code] tags

  6. #6
    Join Date
    Apr 2008
    Posts
    45
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: About new and delete?

    The implicitly shared classes use reference counting. This means that if you make one dynamically (with new) you have to delete it when you are done with it. If you make it statically then Qt handles it.

    The QObject-derived classes will delete their children automatically when the parent is deleted. Works with new or on the stack.

    Objects without a parent (NULL parent pointer) are your responsibility to delete, if not statically created.

  7. #7
    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: About new and delete?

    Quote Originally Posted by Scorp2us View Post
    The implicitly shared classes use reference counting.
    These are mutable - they don't inherit QObject.

    If you make it statically then Qt handles it.
    No, Qt has nothing to do with this. The compiler is responsible for deleting stack based objects.

    The QObject-derived classes will delete their children automatically when the parent is deleted. Works with new or on the stack.
    No, if you create a QObject on the stack and give it a parent, there is a 50% chance deleting the parent will lead to a segfault. Always create parented QObjects on heap.
    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.


  8. #8
    Join Date
    Apr 2008
    Posts
    45
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: About new and delete?

    Er, um, yeah. That's what I meant. I guess that's not what I said.

  9. #9
    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: About new and delete?

    Quote Originally Posted by Scorp2us View Post
    Er, um, yeah. That's what I meant. I guess that's not what I said.
    I'm correcting such things only because uncorrected they lead to myths that are later referenced to as absolute truths ("Hey, Qt is stupid, it deleted my object twice. How lame is that? They write about it here on Qt Centre <and here comes the reference to this thread>").
    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.


  10. #10
    Join Date
    Jun 2009
    Posts
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: About new and delete?

    Thanks of all!!
    Especially andy.fillebrown at #5.
    I'm just a rookie.

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.