On Jun 7, 2008, at 4:24 PM, Johannes Wienke wrote:

Hi,

maybe I've just got a lack of understanding or I don't know...

I've got a class that in general looks like this:

cpdef class Foo:
        cdef char* myData

        cdef void setData(Foo self, char *data)
                self.myData = data

        cpdef doSomething(Foo self)
                print self.myData

The problem is that working with myData directly from Cython is no
problem but calling doSomething from the python interpreter causes a
segfault because self.myData then points to NULL. I've also observed
that self in setData points to a different address than self in
doSomething if it is called from the interpreter.

What's the problem with this approach?


Where are you setting your myData? If you do

    Foo().doSomething()

then it will segfault because the data isn't set yet. If you have

    def set_data_from_python(self, py_string):
        self.myData = py_string

Then the conversion will happen, but myData is set to point to the inside of py_string (i.e. no copying is actually done) and so the instant py_string gets deallocated the actual char* gets reclaimed.

Hopefully this helps, though the code snippet above doesn't crash by itself.

- 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