PDA

View Full Version : QT and delete - puzzling the heck out of me!



kghose
11th August 2008, 20:33
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

jacek
11th August 2008, 23:56
Could you prepare a minimal compilable example?

kghose
12th August 2008, 19:47
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