Results 1 to 12 of 12

Thread: To use or not to use: deleteLater()

  1. #1
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Red face To use or not to use: deleteLater()

    --First off, this might be a Newbie question, but it might not, so feel free to move it.

    Anyways, in my application I have a variety of objects (like QSettings) and I think deleting them after they're used is a good way to free up memory. So, it seems I can't delete an object without an error being generated or my app freezing later.

    So one time, I have an object defined in my header:

    Qt Code:
    1. ...
    2. private:
    3. settings settingsChanger;
    4. ...
    To copy to clipboard, switch view to plain text mode 

    The "settings" class is a QObject subclass that has a single QSettings object defined in the header and contains a variety of members to change values in QSettings. So, in my code after I'm done with settingChanger, naturally I want to delete it. So I use deleteLater() but my program just has a "signal" and the debugger stops it.

    Doing the same thing with a pointer to the object, and having

    Qt Code:
    1. ...
    2. private:
    3. settings *settingsChanger;
    4. ...
    To copy to clipboard, switch view to plain text mode 

    And in the definition:
    Qt Code:
    1. ...
    2. settingsChanger = new settings(this);
    3. ...
    4. settingsChanger->deleteLater()
    5. ...
    To copy to clipboard, switch view to plain text mode 

    causes my program to get a signal too. So, I'm not really sure what I'm doing wrong. Should I delete the QSettings in the destructor of my settings class? I thought deleteLater() was a complete failsafe that wouldn't cause problems like this, but am I wrong, or have I made a huge mistake?

    Thanks in advance for any advice.

    ~codeslicer

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: To use or not to use: deleteLater()

    you should call deleteLater() just like at place where you call delete()...

    deleteLater() can delete your object at any time. You ask qt that you do not need the object and qt can delete it anytime(may be instantly)..

    so

    Qt Code:
    1. object = new Object
    2. object->useIt();
    3. delete object;
    4. object->useIt();//ERROR the object is deleted.
    To copy to clipboard, switch view to plain text mode 

    and this is same as above
    Qt Code:
    1. object = new Object
    2. object->useIt();
    3. object->deleteLater();//Qt will delete it at any time ... may be just now.
    4. object->useIt();//ERROR the object is deleted.
    To copy to clipboard, switch view to plain text mode 

    therefore use deleteLater in a place after which you will never use the object(destructor?)


    EDIT:-
    i was too quick... i just saw u said that you are done with settings object when you call deleteLater().. so may be just check that the object is not refrenced anywhere else
    Last edited by nish; 10th July 2009 at 04:10. Reason: too quick post

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

    codeslicer (10th July 2009)

  4. #3
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: To use or not to use: deleteLater()

    Quote Originally Posted by codeslicer View Post
    So one time, I have an object defined in my header:

    Qt Code:
    1. ...
    2. private:
    3. settings settingsChanger;
    4. ...
    To copy to clipboard, switch view to plain text mode 

    The "settings" class is a QObject subclass that has a single QSettings object defined in the header and contains a variety of members to change values in QSettings. So, in my code after I'm done with settingChanger, naturally I want to delete it. So I use deleteLater() but my program just has a "signal" and the debugger stops it.
    oh i forgot to mention that you cannot delete objects which are on stack

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

    codeslicer (10th July 2009)

  6. #4
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: To use or not to use: deleteLater()

    Quote Originally Posted by MrDeath View Post
    oh i forgot to mention that you cannot delete objects which are on stack
    Wow never knew that. I sort of taught C++ myself so I guess I skipped learning some of the newbie things. So does that mean it's better to make "new" objects because those can be deleted? Or only reserve smaller objects (like bool and int) for the stack?

    I guess I'll check to make sure the object isn't used elsewhere after deleting it.

    But thanks for your help, I'll try that soon...

  7. #5
    Join Date
    Jul 2009
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: To use or not to use: deleteLater()

    Quote Originally Posted by MrDeath View Post
    deleteLater() can delete your object at any time. You ask qt that you do not need the object and qt can delete it anytime(may be instantly)..
    Hmm, I thought that deleteLater() always deletes object on next event loop iteration, so it never happens "instantly" after being issued. Or am I wrong?

  8. #6
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: To use or not to use: deleteLater()

    Quote Originally Posted by dpimka View Post
    Hmm, I thought that deleteLater() always deletes object on next event loop iteration, so it never happens "instantly" after being issued. Or am I wrong?
    deleteLater() can delete your object at any time when the event loop has control, sometimes that can be instantly if there aren't any other events.

  9. #7
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: To use or not to use: deleteLater()

    Quote Originally Posted by codeslicer View Post
    deleteLater() can delete your object at any time when the event loop has control, sometimes that can be instantly if there aren't any other events.
    looks like you learn very fast

    So does that mean it's better to make "new" objects because those can be deleted? Or only reserve smaller objects (like bool and int) for the stack?
    this is a tricky question.
    in qt any class which is derived from qobject can have children(fox example a QDialog containg Ok,Cancel button as its children). Now when the qdialog is deleted, it also deletes all its children,,, if the children are on stack that will cause crash. So all the QObjects derived classes which will become child of any object in your program should be on heap.

    but this is not general rule. many times a local child widget is created on stack(for example a temporary messagebox)

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

    codeslicer (10th July 2009)

  11. #8
    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: To use or not to use: deleteLater()

    Quote Originally Posted by codeslicer View Post
    Wow never knew that. I sort of taught C++ myself so I guess I skipped learning some of the newbie things. So does that mean it's better to make "new" objects because those can be deleted? Or only reserve smaller objects (like bool and int) for the stack?
    Objects allocated on stack are deleted by the compiler once you exit the scope they were declared in. Objects allocated on heap are "persistant" and are not deleted by the compiler - you are responsible for deleting them yourself unless there is some mechanism that does it for you (like a garbage collector or Qt's memory management). So it's actually better to allocate objects on stack because you don't have to care about them being deleted but unfortunately it is not always possible to use stack based objects only.
    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.


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

    codeslicer (10th July 2009)

  13. #9
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: To use or not to use: deleteLater()

    Thanks for the replies, I just have one little question.

    MrDeath said if you delete an object which is on stack and it has children on stack then the program will crash. So the only objects that should be on stack are those that don't have any children?

    Thanks!

  14. #10
    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: To use or not to use: deleteLater()

    Quote Originally Posted by codeslicer View Post
    So the only objects that should be on stack are those that don't have any children?
    No, it's the other way round. The only objects on the stack should be the ones that don't have a parent with exception of QDialog which is a short living object and will be deleted before the scope of its parent ends.
    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:

    codeslicer (11th July 2009)

  16. #11
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: To use or not to use: deleteLater()

    Alright thanks a lot, I really understand how this works. One more thing (sorry :P). I heard it's best to dereference a pointer after the object is deleted. If you use deleteLater() is it necessary to do that? Like here:

    Qt Code:
    1. myObject = new object(this);
    2.  
    3. ...
    4.  
    5. myObject->deleteLater();
    6. myObject = 0;
    To copy to clipboard, switch view to plain text mode 

    Or only use that with delete?

    Thanks once again.

  17. #12
    Join Date
    Jul 2009
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: To use or not to use: deleteLater()

    First: You misunderstood the meaning of the word "dereference" Dereferencing means getting a value of the pointer by using a "*" operator. For example:
    Qt Code:
    1. QObject *obj = new QObject;
    2. qDebug() << *obj; // <-- this is a dereferencing of the "obj" pointer
    To copy to clipboard, switch view to plain text mode 

    Second: Guessing that you asked if it is needed to assign 0 to the pointer after object has been deleted, the answer is: this is handy if your pointer can be potentially used in other places in code (for example if it is a member of some class). In these situations you can check if that object is already deleted. And delete or deleteLater doesn't make difference: Assigning 0 is used only to be able to tell if that ptr still exists or not. Examples:

    Qt Code:
    1. void myFunction()
    2. {
    3. MyClass* obj = new MyClass;
    4. // some action...
    5. delete obj;
    6. // obj = 0 <-- doesn't make sense, function will end and no one will ever know about obj as it is a local variable
    7. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class SomeClass
    2. {
    3. private:
    4. QObject* m_obj;
    5.  
    6. public:
    7. void func1()
    8. {
    9. m_obj = new QObject;
    10. // some action
    11. delete m_obj;
    12. m_obj = 0;
    13. }
    14. void func2()
    15. {
    16. // try to use m_obj, but check if it really exists
    17. if( m_obj )
    18. // do some stuff
    19. else
    20. qDebug() << "oops, m_obj is already deleted";
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by dpimka; 11th July 2009 at 21:47.

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

    codeslicer (12th July 2009)

Similar Threads

  1. Replies: 4
    Last Post: 19th February 2009, 11:10
  2. QT deleteLater again.
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2008, 22:36
  3. Qt Deletelater
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2008, 16:58
  4. DeleteLater works... but why?
    By TheGrimace in forum Qt Programming
    Replies: 11
    Last Post: 6th June 2007, 15:14

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.