Results 1 to 12 of 12

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

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