[Pyrex] Missing Py_DECREF in generated code

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Nov 30 23:44:19 UTC 2006


Igor Khavkine wrote:

> cdef void leak (void *obj):
> 	cdef T t
> 	(<T> obj).j = 1
> 	(<T> obj).i = (<T> obj).i + 1
> 	(<T> obj).a[0] = (<T> obj).a[0] + 1   # <-- Missing Py_DECREF
> 	t = <T> obj
> 	t.a[0] = t.a[0] + 1

Is there any reason you couldn't write this as

   cdef void leak (T obj):
     obj.j = 1
     obj.i = obj.i + 1
     obj.a[0] = obj.a[0] + 1

The reference counting behaviour of typecasts involving
object references is not well defined, and you do it at
your own risk. It's much better to declare things as
having the appropriate type wherever possible.

If you really must use a typecast, it's safest to just
do one of them and put the result into an appropriately
typed local as soon as possible, e.g.

   cdef void leak (void *obj):
     cdef T t
     t = <T>obj
     t.j = 1
     t.i = t.i + 1
     t.a[0] = t.a[0] + 1

--
Greg



More information about the Pyrex mailing list