On Fri, Mar 12, 2010 at 7:46 PM, Ondrej Certik <[email protected]> wrote:
> Hi Lisandro!
>
> On Fri, Mar 12, 2010 at 7:25 PM, Lisandro Dalcin <[email protected]> wrote:
>>>>
>>>> Can you explain me how are you managing reference counting? I think
>>>> that you will need a proxy C++ class to manage incref()/decref()
>>>
>>> Here are the definitions of py2c_int and c2py_int:
>>>
>>> cdef api object c2py_int(int i):
>>>    return i
>>>
>>
>> This function will return a brand new object (or a new reference of a
>> caches Python intinstance, this is a CPython implementation detail)...
>> Anyway, if you call this from C/C++, you have to somehow take
>> ownership of that object, of use it and throw it away (i.e. decref)
>>
>>>
>>> cdef api int py2c_int(object i):
>>>    return i
>>>
>>
>> This functions just take a (borrowed) object as its argument...
>>
>> So if you do:
>>
>> py2c_int( c2py_int(7) )
>>
>> you have just leaked a reference :-) ...
>
> Yep. But the c2py_int() is only meant to use with insert_object(),
> (that I'll rename to push):
>
> cdef api void insert_object(const_char_p name, object o):
>     global_namespace.update({name: o})
>
> where global_namespace is a dictionary. All in Cython. Example:
>
> insert_object("a", c2py_int(3));
>
> will this create a leak? e.g. if I do this in Cython:
>
> insert_object("a", c2py_int(3))
>
> there should be no leak, right? Will it create a leak when executed
> from C++? I am not sure now, I'd have to test it.

the Cython version produces this C code:

  __pyx_t_1 = c2py_int(7);
  insert_object(__pyx_k__a, __pyx_t_1);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

So indeed, I think I have a leak if I just do:

  insert_object(__pyx_k__a, c2py_int(7));

What's the best way to fix it? Calling Py_DECREF() by hand in
insert_object and adding a strong warning in the comments, that it is
ok to call "insert_object(__pyx_k__a, c2py_int(7))" from C, but one
should never do that in Cython?

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

Reply via email to