Results 1 to 3 of 3

Thread: QT and delete - puzzling the heck out of me!

  1. #1
    Join Date
    Jul 2008
    Location
    East Coast, USA
    Posts
    40
    Thanks
    6
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question QT and delete - puzzling the heck out of me!

    Hi,

    I have a regular class A and a class B that inherits from a QGraphicsWidget. class B carries a pointer to A.

    class A counts itself - that is a given instance of A can be shared by several other classes. An instance of A can be "attach"-ed to other classes (such as B). When an object pointing to an instance of A gets destroyed it calls "detach". When an instance of A finds that the last call to "detach" has left it with no parents, it deletes itself.

    So one would expect that if I create an object of class B and attach an object of class A to it and then delete B first A should be destroyed then B is destroyed.

    Now when I try out this logic with straight C++ it works fine. However, when I track my QT based program I see that things happen in batches. That is, I have a list of As and a list of Bs. When I run through the list of Bs deleting them, the Bs get destroyed first as a group and then the As get destroyed second AS A GROUP.

    The way I'm noting this is to have print statements in the code. What is happening? Are the delete statements somehow queued when we deal with a Q_OBJECT?

    I would really appreciate this puzzler being cleared up

    Thanks
    -K

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT and delete - puzzling the heck out of me!

    Could you prepare a minimal compilable example?

  3. #3
    Join Date
    Jul 2008
    Location
    East Coast, USA
    Posts
    40
    Thanks
    6
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Solved! Re: QT and delete - puzzling the heck out of me!

    Heh - I found out what was happening:

    I forgot that in addition to what I mentioned I have another container C that also has pointers to A. So when I delete a bunch of B, A is not destroyed, because C still has ownership. When I finally delete C, the 'A's are then deleted, IN A BATCH.

    Mystery solved. A bit like one of those ghost stories, where the ghost turns out to be a cat...

    -K

Similar Threads

  1. c++, placement delete upon exception
    By stinos in forum General Programming
    Replies: 6
    Last Post: 31st October 2006, 15:38

Tags for this Thread

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.