[capi-sig] Problem with reference

Stefan Behnel python_capi at behnel.de
Tue Mar 3 08:16:39 CET 2009


pycnweb wrote:
> I am now writing a python extension, but I confuse with the reference.

If you care more about working code than fighting confusion, Cython might
be worth a look.

http://cython.org


> PyTuple_SetItem and PyList_SetItem will steal a reference from item. So
> 
> PyTuple_SetItem(pTuple, 2, Py_BuildValue("s", "Python"))
> PyList_SetItem(pList, i, Py_BuildValue("i", i))
> 
> will not cause a memory leak.

Correct.


> But
> 
> PyList_Insert(pList, 2, Py_BuildValue("s", "inserted"))
> PyList_Append(pList, Py_BuildValue("s", "appended"))
> PyDict_SetItemString(pDict, "second", Py_BuildValue("f", 3.14f))
> 
> will cause a memory leak.

Yes.

Stefan



More information about the capi-sig mailing list