On Jul 27, 2009, at 3:41 PM, Greg Kochanski wrote:

> I'd like to propose that if an extension class has a
> __cinit__() member, it must have a __dealloc__()
> member.  The compiler should raise an error
> or at least a warning.

Certainly not an error--maybe a warning.

> I realize this will annoy a few people in the odd case or
> two where one isn't necessary, but the problem is
> that it is all too easy to write code like this:
>
>
> cdef class foo:
>    cdef double *x
>
>    def __init__(self, whatever):
>          stuff
>
>   def __cinit__(self, whatver):
>         self.x = <double *>calloc(...)
>
>   def __deallocate__(self):
>        free(self.x)
>
>
> Such a class works perfectly, until you notice the memory
> leak.    (Because, __deallocate__() isn't a special member
> even though the name is very suggestive, so free() never
> gets called.)
>
> I've done this twice now.   The first one required several hours
> of debugging and a bicycle ride in the rain to reboot my
> thrashing computer at work.   The second one took less time,
> but it's still a very easy mistake to make and an annoying one
> to find.
>
> People might also think of __cdel__/__cinit__ by analogy with
> python's __del__/__init__.

I don't think the analogy is quite as direct, because __del__ is  
optional, rare, and slightly discouraged.

- Robert


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

Reply via email to