[C++-sig] Custodian and Ward doubts

Niall Douglas s_sourceforge at nedprod.com
Tue Sep 13 21:27:20 CEST 2005


On 13 Sep 2005 at 11:02, Eric Jardim wrote:

> However, to answer your question, what you need is a mechanism that >
> when a Qt object dies, it upcalls to its BPL equivalent that it is >
> dead now so the BPL equivalent can mark itself as useless. 
> 
> That's easy. This could be implemented as a virtual function, but that
> would only handle Python wrappers (my custom QObject extensions). I
> wanto also to handle C++ pure object (i hope you understand what I am
> talking about).

Yes you need the virtual function in the C++ object, not the python 
wrapper.

> The good news is that Qt4 QObjects have a signal "destroyed" that
> informs when *any* kind of object is deleted. So, this could be used
> to do the "upcall".

If that signal gets sent during the QObject destructor then it's no 
better (your virtual function table is still dead). And I'd kinda 
have to think that must be the case as who knows what the subclass 
does to destroy itself.

> How can a Python object be "useless"? I mean, if I have "a" and "b"
> objects, where "a" is "b" parent, if I delete "a", then "b" is also
> deleted. But if I still have a reference to "b", what would it be
> holding? None?

I forgot to mention last msg that in the code in FXShell in TnFOX 
where FXShell deletes the child windows, I added code which first 
calls that detach from BPL virtual function.

Sorry right now is not the best time. I'm moving country tomorrow 
morning.

> You can't
> > delete the BPL equivalent because python maintains exclusive rights
> > to do that, but you must make sure that when python does delete it
> > it doesn't go trying to redelete your Qt object.
> 
> Hmm, sure. That's what I am trying to do. One solution I thought is to
> detach the children (in Python's __del__) before the parent really
> dies (in C++ destructor). This would prevent the children from being
> deleted. If the children refcount to 0 they would be deleted alone.

I don't see how this helps. If Qt deletes a child and then python 
tries to access it, you still get a segfault.

> The way I handled this in TnFOX is to add a special virtual method to
> > FXObject which detaches that FXObject instance from its BPL >
> representation - if it has one. 
> 
> 
> How do you do this? That's the point.

You wrap auto_ptr's to the QObject subclasses. You simply release the 
auto_ptr to detach.
 
> Yes, you helped. Instead, I don't see any "auto_ptr" on you
> implementation. How are you holding you references?

I patch pyste to generate the appropriate wrapper code. See 
BoostPatches.zip in the TnFOX SVN.

As of tonight I'll be offline for a few days until I get set up in 
Scotland, but I'll reply when next I can.

Cheers,
Niall






More information about the Cplusplus-sig mailing list