[Pyrex] Missing Py_DECREF in generated code

Stefan Behnel behnel_ml at gkec.informatik.tu-darmstadt.de
Fri Dec 1 09:27:38 UTC 2006


Hi,

Igor Khavkine wrote:
> On 11/30/06, Greg Ewing wrote:
>> 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
> 
>> 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
>> 
>> The reference counting behaviour of typecasts involving
>> object references is not well defined, and you do it at
>> your own risk.
> 
> That's somewhat misleading. Each line in my example, except the
> indicated one, produces correct code. I think anyone, who's seen that
> the first two assignments in leak() work properly, would expect the
> third one to work as well. That's why it's a bug.

Note that very few people would come up with your implementation in the first
place, one reason being the low readability of the code (most likely the real
name of 'T' is not as short as in your example, right?), a more important
reason being the tremendous overhead of having to do the ref-counting multiple
times for the same reference. So, just for performance sake: use the more
readable variant.

Stefan




More information about the Pyrex mailing list