Py_INCREF() incomprehension

Thomas Rachel nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at
Tue Apr 26 08:21:19 EDT 2011

Am 26.04.2011 11:48, schrieb Ervin Hegedüs:

> Everything works fine, but sorry for the recurrent question: where
> should I use the Py_INCREF()/Py_DECREF() in code above?

That depends on the functions which are called. It should be given in 
the API description. The same counts for the incoming parameters (which 
are borrowed AFAIR - but better have a look).

The most critical parts are indeed

* the input parameters


* Py_BuildValue()

. Maybe you could as well have a look at some example code.

Especially look at the concepts called "borrowed reference" vs. "owned 

And, for your other question:

 > if (cRes == 0) {
 >       return Py_BuildValue("s", outdata);
 > }

You ask how to stop leaking memory? Well, simply by not leaking it :-)

Just free the memory area:

if (cRes == 0) {
     PyObject* ret = Py_BuildValue("s", outdata);
     return ret;

BTW: Is there any reason for using calloc()? malloc() would probably be 


More information about the Python-list mailing list