[PyKDE] Help on wrapping a C library

Phil Thompson phil at riverbankcomputing.co.uk
Tue May 24 17:20:33 BST 2005


On Tuesday 24 May 2005 4:40 pm, Huaicai Mo wrote:
> > -----Original Message-----
> > From: Phil Thompson [mailto:phil at riverbankcomputing.co.uk]
> > Sent: Tuesday, May 24, 2005 3:45 AM
> > To: pykde at mats.imk.fraunhofer.de
> >..........
> > In your implementation of reverse() you are freeing the memory allocated
> > to
> > the Word structure and the word itself. Because the wrapper has ownership
> > it
> > will also try to free the memory which will result in the seg fault.
> >
> > Either remove the calls to free() or add /Transfer/ to the argument to
> > reverse().
> >
> > Phil
>
> Thank you Phil for the reply. I did try as you suggested by removing the 2
> free() calls in the reverse() or just simply implement reverse() by
>
> 		return word->the_word;
>
> I also tried adding the /Transfer/ annotation to the argument of reverse()
> in the word.sip file without removing the 2 free() calls. I still get the
> segmentation fault.
>
> BTW, here is one of the generated functions in sipwordWord.c that I guess
> is used by the wrapper to free the memory:
>
>     static void dealloc_Word(sipWrapper *sipSelf)
>     {
> 		If(sipIsPyOwned(sipSelf))
> 		  sipFree(sipSelf -> u.cppPtr);
>     }
>
> I am not sure if the above function is able to free "the_word" member in
> the "Word" structure, so I tried keeping "free(word->the_word);" in the
> reverse() too, it doesn't help either. For the whole generated C files,
> please see the attachment.

Any memory pointed to by "the_word" member won't get freed. As of tonight's 
SIP snapshot you will be able to specify ctors and dtors in C structures to 
allow you to write handwritten code to deal with situations like this.

There is a bug in the example in the documentation - create_word() should have 
a /Factory/ annotation. This should only cause a memory leak - not the 
problems you are having.

Can you send me the word.c, word.h, word.sip you are currently using and a 
Python script that demonstrates the problem? (And a Makefile if you have one 
handy.)

Phil




More information about the PyQt mailing list