Py_INCREF() incomprehension

Hegedüs Ervin airween at gmail.com
Tue Apr 26 16:03:21 CEST 2011


Hello,

thanks for the answer,

> >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).

I've read API doc (which you've included in another mail), but
that's not clear for me. :(
 
> The most critical parts are indeed
> 
> * the input parameters
> 
> and
> 
> * Py_BuildValue()
> 
> . Maybe you could as well have a look at some example code.
> 
> Especially look at the concepts called "borrowed reference" vs.
> "owned reference".
> 
> 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 :-)

great! :)
 
> Just free the memory area:
> 
> if (cRes == 0) {
>     PyObject* ret = Py_BuildValue("s", outdata);
>     free(outdata);
>     return ret;
> }

so, it means when I implicit allocate a new object (whit
Py_BuildValue()), Python's GC will free that pointer when it
doesn't require anymore?
 
> BTW: Is there any reason for using calloc()? malloc() would probably
> be faster...

may be, I didn't measure it ever... but calloc() gives clear
space... :)


thanks:

a.



More information about the Python-list mailing list