On Apr 20, 2008, at 1:03 PM, Johannes Wienke wrote:
Hi again,

Am 04/19/2008 12:37 AM schrieb Robert Bradshaw:
Try doing something like

cdef extern from *:
    ctypedef char* const_char_ptr "const char*"

cdef foo_c(const_char_ptr s):
    print s

foo_c("yo")

I've found a new problem with this approach. I've declared a function

cdef public void plug_observ_data(plugDefinition* p, const_char_ptr i):
        print "Plugin '%s' ... type '%s'" % p.name, i

Compiles without a problem but results in a segfault when this function
gets called from a C lib. Output is generated and afterwards the
segfault occurs. Seems to be related to the conversion to Python
objects, because if I use a standard printf there is no problem. Every
other operation that requires a conversion to Python objects results in
a segfault to.

To my surprise it seems to be unrelated to the const thing. Not using i
and only doing some conversions with p results in the same problem.

Is there any chance to get this working? I've tried reading the C code
but that's nearly impossible for me.

When you write

    cdef char* s = x

where x is a Python object, it sets s to a pointer of the data of s. This is why you don't have to worry about allocating/deallocating s. However, if you try to use s after x has gone out of scope/has been garbage collected, s may contain garbage. Perhaps this is what's happening here, and if that's the case you need to allocate s manually and strcpy the contents of the string over.

- Robert

Attachment: PGP.sig
Description: This is a digitally signed message part

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to