Is this a leaker?

Thomas Wouters thomas at xs4all.nl
Fri Jun 30 09:08:55 EDT 2000


On Fri, 30 Jun 2000 13:00:44 GMT, Arinté <shouldbe at message.com> wrote:
>Let me expand this a little more
> PyObject* deqme(Py_BuildValue("i",0));
> PyObject* comm=PyObject_GetItem(args, deqme);
> Py_XDECREF(deqme);

>comm isn't decref until later should I wait to decref deqme after I do
>comm??

No. 'deqme' is simply used to find 'comm', but isn't necessary to keep
'comm' valid. And if it was, 'comm' would INCREF deqme itself. (Or, in the
extreme case, it would be documented as 'Stealing' the reference to 'deqme')

>Also, here is a painful question Who is responsible for
>return Py_BuildValue("i",0);
>I skimmed thru python's source and it seems they Incref's on return values
>so now it seems it is leaked.

This depends on how you document your function :) If you document it as
returning a borrowed reference, the caller should not DECREF the returned
value. If you document it as returning a new reference, it should take care
of the DECREF itself. In this case, it doesn't make sense to return a
borrowed reference: If you do a DECREF on the fresly-created
Py_BuildValue()-object, it'll end up destroyed !




More information about the Python-list mailing list