Hi Phil,

I am sending you the files: word.h, word.c, word.sip, configure.py, mytest.c
and clib_makefile(the make file used to create the C library and the test
program). After creating the C library, I added the line 
              /usr/local/lib
to the /etc/ld.so.conf file, and run:
           install word*.so /usr/local/lib
           ldconfig

I don't have a python script to test the extension module. I tested
interactively. Here is the snapshot:

Python 2.3.4 (#2, Aug 19 2004, 15:49:40) [GCC 3.4.1 (Mandrakelinux (Alpha
3.4.1-3mdk)] on linux2 Type "help", "copyright", "credits" or "license" for
more information.
>>> import word
>>> print word.anotherReverse("sadkfj;sadf")
fdas;jfkdas
>>> print word.__dict__
{'create_word': <built-in function create_word>, 'Word': <class
'word.Word'>, 'reverse': <built-in function reverse>, '__file__': 'word.so',
'anotherReverse': <built-in function anotherReverse>, '__name__': 'word',
'__doc__': None}
>>> w = word.create_word("How are you?") 
>>>print w
<word.Word object at 0x4038fddc>
>>> print word.reverse(w)
Segmentation fault

Thank you very much!

Regards,
Huaicai


> -----Original Message-----
> From: Phil Thompson [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, May 24, 2005 12:21 PM
> To: [email protected]
> Cc: Huaicai Mo
> Subject: Re: [PyKDE] Help on wrapping a C library
> 
> On Tuesday 24 May 2005 4:40 pm, Huaicai Mo wrote:
> > > -----Original Message-----
> > > From: Phil Thompson [mailto:[EMAIL PROTECTED]
> > > Sent: Tuesday, May 24, 2005 3:45 AM
> > > To: [email protected]
> > >..........
> > > 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

Attachment: clib_makefile
Description: Binary data

Attachment: mytest.c
Description: Binary data

Attachment: word.c
Description: Binary data

Attachment: word.h
Description: Binary data

Attachment: word.sip
Description: Binary data

import os
import sipconfig

# The name of the SIP build file generated by SIP and used by the build
# system.
build_file = "word.sbf"

# Get the SIP configuration information.
config = sipconfig.Configuration()

# Run SIP to generate the code.
os.system(" ".join([config.sip_bin, "-c", ".", "-b", build_file, "word.sip"]))

# Create the Makefile.
makefile = sipconfig.SIPModuleMakefile(config, build_file)

# Add the library we are wrapping.  The name doesn't include any platform
# specific prefixes or extensions (e.g. the "lib" prefix on UNIX, or the
# ".dll" extension on Windows).
makefile.extra_libs = ["word"]

# Generate the Makefile itself.
makefile.generate()
_______________________________________________
PyKDE mailing list    [email protected]
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde

Reply via email to