[PyKDE] PyQt4 problems with underlying C/C++ objects [weird]

Phil Thompson phil at riverbankcomputing.co.uk
Tue Nov 21 15:15:53 GMT 2006


On Tuesday 21 November 2006 2:57 pm, Krystian Samp wrote:
> Hi,
> thanks for the answer. When I commented the line which adds MyItem2 to the
> scene the effect was the same - the underlying C/C++ object was destructed.
> It is weird but I found some interesting notes on this problem:
> http://www.nanoengineer-1.net/mediawiki/index.php?title=Qt_4_mysteries#One_
>mystery_solved
>
> It seems that when you create an item the python object holds a reference
> to it. However, if you assign that item to a hierarchy of items (for
> instance using setParentItem()) the python object loses (or you can say
> hands over) the reference. This reference still exists in the C/C++ level
> in the hierarchy but only there. So If you stop referencing to the parent
> of the item then the item itself will be destroyed as well. This is weird
> since you still have a python object representing the item but as I said it
> handed over the reference to the C/C++ layer. This is my observation and I
> think it's not a good behavior of PyQt since you actually have to think
> about C/C++ and the principle I've described.
>
> so for the above code snippet it is sufficient to detach children items
> from the parent before deleting the parent.
>
> any comments? your observations?

The behaviour you describe is deliberate and required to avoid segfaults when 
two pieces of code both think they have responsibility for calling a C++ 
instance's dtor.

Phil




More information about the PyQt mailing list