[Cython] __dealloc__ called even if __cinit__ failed

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Oct 6 06:31:56 CEST 2015


Jeroen Demeyer wrote:
> The problem is that __cinit__ is supposed to set up the C-level 
> attributes for that object. If this somehow fails, then the object isn't 
> in a consistent state yet, so it makes no sense to call __dealloc__ on it.
> 
> Do you consider this a Cython bug or should I manually protect my code 
> against this?

It's probably a good thing that __dealloc__ is called,
because it provides an opportunity to clean up a partially
initialised object that would otherwise leak memory.

It does mean that you need to design your __dealloc__
to be robust against this situation. However, the
alternative would be to catch exceptions in __cinit__
and clean up there, which means you have cleanup code
in two places.

-- 
Greg


More information about the cython-devel mailing list