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

Phil Thompson phil at riverbankcomputing.com
Thu Feb 20 09:07:40 GMT 2020


I meant the C++ destructor, not __dtor__.

Phil

On 20/02/2020 02:20, Weitian Leung wrote:
> But nothing changes as I added the __dtor__
> 
> // typedef unsigned short BSTR;
>> %MappedType BSTR /TypeHint="str",TypeHintValue="''"/
>> {
>> %ConvertToTypeCode
>> ...
>> %End
>> 
>> %ConvertFromTypeCode
>> ...
>> %End
>> };
>> void __dtor__();
>> %MethodCode
>>     printf(">>>>>> __dtor__\n");
>> %End
>> 
> 
> If place the __dtor__ inside the MappedType block, got syntax error 
> when
> build...
> Repace __dtor__ with __del__ is the same. Seems that __dtor__ is for
> struct/class only?
> 
> I can't find more information from the SIP docs.
> 
> On Thu, Feb 20, 2020 at 12:24 AM Phil Thompson 
> <phil at riverbankcomputing.com>
> wrote:
> 
>> Have a look at %MethodCode for dtors.
>> 
>> Phil
>> 
>> On 19/02/2020 14:19, Weitian Leung wrote:
>> > One more question, is it possible to insert handwritten code to the
>> > MappeedType's release_XXX?
>> >
>> > When mapped one type to python's str, which type has it's own alloc
>> > method
>> > (The BSTR for example, SysStringAlloc etc). After passed to the C++
>> > call
>> > and returned, we need to free the memory using it's own dealloc method.
>> >
>> > So is it possible do that? Or other way to call the dealloc method...
>> >
>> > On Wednesday, February 19, 2020, Weitian Leung <weitianleung at gmail.com>
>> > wrote:
>> >> Thanks, it works!
>> >>
>> >> On Wednesday, February 19, 2020, Phil Thompson <
>> > phil at riverbankcomputing.com> wrote:
>> >>> 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