[Cython] Debugging a segfault in a cython generated module
Stefan Behnel
stefan_ml at behnel.de
Sat Apr 19 11:22:06 CEST 2008
Hi,
Brian Granger wrote:
> I am realizing that this is also a problem. I was creating a memory
> leak by having both the subclass and superclass do allocations in
> their own __cinit__. The trick you propose:
>
> cdef class Foo:
>
> cdef __cinit__(self):
> self._actual_cinit(self)
> cdef _actual_cinit(self):
> foo_ptr = doTheAllocation()
>
> cdef class Bar(Foo):
>
> cdef _actual_cinit(self):
> bar_ptr = doTheAllocation()
>
> When I try this pattern Foo's _actual_cinit *always gets called.* I
> looked at the generated C code and it look like this:
>
> __pyx_1 = ((struct __pyx_vtabstruct_6txbase_Foo *)((struct
> __pyx_obj_6txbase_Foo *)__pyx_v_self)->__pyx_vtab)->_actualCinit()
>
> When I made _actualCinit(), I got an attribute error (which makes
> sense as the Python side of the class has not been built yet.)
>
> Why is the vtable not finding the right _actualCinit implementation?
> Is this a bug, or is this to be expected because the class has not
> been fully built.
Hmmm, this should work (but in case vtab is not initialised correctly at this
point, I would consider that a bug).
> Also, why can't I just do the allocations in the __init__ method?
> That has a much more flexible and explicit approach when dealing with
> subclasses.
The call to __init__() is a Python thing. Imagine a subclass that overrides
__init__ and forgets to make a super() call.
Stefan
More information about the Cython-dev
mailing list