Reference counting problems?

MRAB python at mrabarnett.plus.com
Thu Dec 9 18:50:31 EST 2010


On 09/12/2010 20:23, Eric Frederich wrote:
> I am attempting to automate the building of binding for a 3rd party library.
> The functions I'm wrapping all return an integer of whether they
> failed and output are passed as pointers.
> There can be multiple return values.
> So the code that I generate has a PyObject* called python__return_val
> that I use for returning.
> In the 'wrapped_foo' function below you see I build the return value
> with Py_BuildValue and "OO" as the format.
> For every output I have I do a C to Python conversion and add another
> 'O' to the format.
> What I'm wondering is if I can use the same logic when wrapping
> functions that only return one value like the wrapped_bar function
> below.
>
> So for multiples, I wind up doing this which is fine.
>
>      python__x = Py_BuildValue("s", x)

The object at python__x has a refcount of 1.

>      python__y = Py_BuildValue("s", y)

The object at python__y has a refcount of 1.

>      python__return_val = Py_BuildValue("OO", python__x, python__y);

The object at python__return_val (a tuple) has a refcount of 1. This
object refers to the objects at python__x and python__y, so both of
their refcounts will have been incremented to 2. You will need to
DECREF the objects at python__x and python__y before the C variables
python__x and python__y go out of scope.
>
> But for single returns I do something like this....
> I realize that the 2 lines below are pointless, but are they causing a
> memory leak or problems with reference counting?
>
>      python__x = Py_BuildValue("s", x)

The object at python__x has a refcount of 1.

>      python__return_val = Py_BuildValue("O", python__x);

In this case Py_BuildValue returns its argument python__x, but will
have incremented its refcount to 2. This means that you still need to
DECREF the object at python__x before the C variable python__x goes out
of scope.

This is probably just for consistency, because it's easier to remember
that /every time/ you pass an object to Py_BuildValue its refcount will
be incremented, than if it was sometimes yes, sometimes no.
>
>
> Are python__x and python__return_val the same object, a copy of the object?
> Would python__x ever get garbage collected?
> Should my code generator detect when there is only one output and not
> go through the extra step?
>
[snip]



More information about the Python-list mailing list