[PyQt] how does sip detect when an object has been deleted by C++
Phil Thompson
phil at riverbankcomputing.com
Wed Mar 17 09:15:43 GMT 2010
On Tue, 16 Mar 2010 20:57:14 -0700, Nathan Cournia <cournia at gmail.com>
wrote:
> Hi,
>
> I've just started using SIP and have run into some questions. First,
given
> the following pseudo sip code:
>
> struct Foo
> {
> %TypeHeaderCode
> #include <Foo.h>
> %End
>
> void bar();
> };
>
> // basically does "return new Foo;"
> Foo* createFoo()
>
> // basically does "delete foo;"
> deleteFoo(Foo* foo /Transfer/);
>
> Now let's look at some pseudo python code:
>
> # creates a Foo* object
> a = createFoo()
>
> # deletes the created Foo* object
> deleteFoo(a)
>
> # returns false
> sip.isdeleted(a)
>
> # crash
> a.foo()
>
> I would have expected sip.isdeleted(a) to return true.
>
> If I modify the definition of Foo to inherit from QObject like so:
>
> struct Foo: QObject
> {
> %TypeHeaderCode
> #include <Foo.h>
> %End
>
> void bar();
> };
>
> with Foo inheriting from QObject, the code above will return true at
> sip.isdeleted(a). Neat!
>
> Which brings me to my question: How does SIP detect that the C++ code in
> deleteFoo() deleted the QObject inherited Foo object? Could you point me
> to
> the code which does this detection? I've looked over the documentation
and
> source code, but haven't found anything describing the actual mechanism.
>
> My hope is to understand how this detection works, and possibly use
> something similar to track when objects are deleted without having to
> inherit from QObject.
In order to detect when C++ deletes an object the object has to have a
virtual dtor.
Phil
More information about the PyQt
mailing list