Results 1 to 12 of 12

Thread: DeleteLater works... but why?

  1. #1
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Smile DeleteLater works... but why?

    I have been debugging a multi-threaded application for a while now.

    Recently I had been getting Heap corruption breakpoints from VS .net and had been trying to locate the source.

    I found the source in a widget that deleted another widget directly. Apparently some part of the code was still pointing to it after i deleted it. (It wasn't any of my written code, but it may have been Qt or MS generated)

    Anyway, on a hunch i tried deleteLater for the sub-widget, and it worked like a charm. (No more heap corruption errors at any rate)

    I am wondering if there is any reason deleting a widget directly is bad in a multi-threaded application?

    (Can't post any of my code as it does not belong to me. It belongs to my company)

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: DeleteLater works... but why?

    A QObject automatically deletes all its children when itself is deleted.

    So, I assume that after you deleted that widget, and it deleted it's children, there remained some references throughout the app to the invalid children.
    Therefore it does not matter that the app is multithreaded or not.

    Why not "deleteLater" the parent widget?

    Regards

  3. #3
    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: DeleteLater works... but why?

    Did you try to delete the object from within a slot that took it as an argument?

  4. #4
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: DeleteLater works... but why?

    I should explain more specifically:

    I am placing a new Widget pointer (call it widgetA) in a struct-like QObject class. (all data members public)

    That widgetA performs a function, sends a signal to the struct-like Qobject's creator, who then deletes the widgetA and the struct-like widget.

    The signal being received is a unique variable to that object that allows me to search a list for the object while under the protection of a mutex locker.

    In this case the VS .NET would complain on the very next new memory allocation about a heap corruption.

    If I use deleteLater on the widgetA and then also on the struct-like Qobject, it no longer has this issue.
    Last edited by TheGrimace; 5th June 2007 at 20:42. Reason: updated contents

  5. #5
    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: DeleteLater works... but why?

    Could you please answer my question?

  6. #6
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: DeleteLater works... but why?

    Quote Originally Posted by TheGrimace View Post
    The signal being received is a unique variable to that object that allows me to search a list for the object while under the protection of a mutex locker.
    As I said, I am receiving a unique variable (in this case an integer) so the answer to your question is no.

  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: DeleteLater works... but why?

    But you receive from where? Maybe in the chain of calls the object that gets deleted in one of "subslots" is passed as a signal parameter at some point?

  8. #8
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: DeleteLater works... but why?

    I never pass any objects by reference in this particular case.

    I receive the signal emitted from widgetA into the object that deletes it. The signal contains a unique integer passed by value. I believe Qt::AutoConnection turns the connection into a direct connection.

  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: DeleteLater works... but why?

    Let me explain... Imagine we have a following situation:
    Qt Code:
    1. class A : public QObject{
    2. //...
    3. signals:
    4. void sigA(QObject *);
    5. };
    6. class B : public QObject{
    7. //...
    8. signals:
    9. void sigB(int);
    10. public slots:
    11. void slotB(QObject *o){ emit sigB(m_list.indexOf(o)); }
    12. };
    13.  
    14. class C : public QObject {
    15. //...
    16. public slots:
    17. void slotC(QObject *o){...}
    18. };
    To copy to clipboard, switch view to plain text mode 
    Now imagine we connect sigA to slotB and slotC and sigB to some slot that deletes the item pointed by the integer emited from sigB. If sigA is emitted, slotB is called and emits sigB which in turn calls a slot that deletes the item. Seems fine... But then slotC is called and tries to operate on the same object. But it has already been deleted! Hence the crash. Now if you use deleteLater(), the object is deleted only after all slot have been called. That's probably the problem you're facing, but I can't be sure without seeing your code.

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

    TheGrimace (6th June 2007)

  11. #10
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: DeleteLater works... but why?

    Would I get the same same result if the instance of A&C were inside of B?

    IE

    A&C -> signals -> B

    B -> signals -> D (the object that created B) to delete it.

    but A and C are new instances of those classes created in B.

    kind of like:

    Qt Code:
    1. class A : public QObject{
    2. //...
    3. signals:
    4. void sigA(QObject *);};
    5. class B : public QObject{
    6. //...
    7. B() { a= new A(); b=new B(); connect(AtoB); connect(CtoB);}
    8. signals:
    9. void sigB(int);
    10. public slots:
    11. void slotB(QObject *o){ emit sigB(m_list.indexOf(o)); }
    12. };
    13. class C : public QObject {
    14. //...
    15. public slots:
    16. void slotC(QObject *o){...}
    17. };
    18. class D : public QObject {
    19. //...
    20. public:
    21. D() { b = new B(); connect(B to D);}
    22. public slots:
    23. DSlot() { delete b or deletelater b}
    24. };
    To copy to clipboard, switch view to plain text mode 

    and B initiates a call to the D Slot which deletes it and its children.

  12. #11
    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: DeleteLater works... but why?

    The relationship doesn't matter. The only thing that matters is that more than one slot operates on the same object that gets deleted in one of the slots. If that is the case, you have to use deleteLater() or somehow check if the pointer is stil valid (checking for null won't do).

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

    TheGrimace (6th June 2007)

  14. #12
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: DeleteLater works... but why?

    That is most likely what was going wrong then.

    Thank You for your help.

Similar Threads

  1. How signal-slot works across DLL and application???
    By Shuchi Agrawal in forum Newbie
    Replies: 4
    Last Post: 15th May 2007, 11:24
  2. Replies: 3
    Last Post: 28th January 2007, 17:24
  3. How setEnabled() works on QFrames.
    By Doug Broadwell in forum Newbie
    Replies: 4
    Last Post: 18th October 2006, 19:55
  4. Replies: 3
    Last Post: 1st October 2006, 15:03
  5. How simpletreeview example works?
    By igorko in forum Qt Programming
    Replies: 1
    Last Post: 19th June 2006, 09:59

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.