[PyQt] How to write handwritten code to access the C++ object when the corresponding python object destroyed?

Phil Thompson phil at riverbankcomputing.com
Tue Feb 18 17:28:05 GMT 2020


On 17/02/2020 14:35, Weitian Leung wrote:
> Hi,
>     I'm noob in SIP. Say I have two abstract interfaces and one method 
> to
> create the object:
> 
> struct IUnknown /Abstract/
>> {
>>     virtual void AddRef() = 0;
>>     virtual void Release() = 0;
>> 
>>     %GCClearCode
>>         printf("GCClearCode\n");
>>     %End
>> };
>> 
>> struct IDemo : public IUnknown /Abstract/
>> {
>>     virtual void SayHi() = 0;
>> };
>> 
> 
> void createDemo(IDemo **demo /Out/);
>> 
> 
> 
> Using in python side:
> idemo = demo.createDemo()
> idemo = None
> 
> Is it possible to write some handwritten code that call the Release 
> method
> when idemo set to None or when  idemo doing GC?
> Or if we can call AddRef automatically when idemo assign to another 
> object.
> 
> I known that on python side we can call AddRef/Release to make it 
> works,
> but I want the code more python's way.
> 
> The %GCClearCode handwritten code never called in this case.
> And the %GCTraverseCode handwritten code only call when the object make 
> an
> assignment to other object.
> Am I missing something?
> 
> Please check the full code in the  attached file.

https://www.riverbankcomputing.com/static/Docs/sip/c_api.html#event-handlers

I've only just realised that that documentation is incomplete. The two 
event types currently implemented are...

sipEventWrappedInstance,    /* After wrapping a C/C++ instance. */
sipEventCollectingWrapper,  /* When garbage collecting a wrapper object. 
*/

...and the corresponding event handler signatures are...

typedef void (*sipWrappedInstanceEventHandler)(void *sipCpp);
typedef void (*sipCollectingWrapperEventHandler)(sipSimpleWrapper 
*sipSelf);

Phil


More information about the PyQt mailing list