Problem with reference
Hi All,
I am now writing a python extension, but I confuse with the reference.
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. 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.
Am I right?
Thanks.
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.
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
Stefan Behnel <python_capi@behnel.de> writes:
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.
Stefan, please stop doing this. This list is about helping people using Python/C, not Cython advocacy.
Hrvoje Niksic wrote:
Stefan Behnel writes:
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.
Stefan, please stop doing this. This list is about helping people using Python/C, not Cython advocacy.
Where is the difference?
Stefan
2009/3/3 pycnweb <pycnweb@126.com>
Hi All,
I am now writing a python extension, but I confuse with the reference.
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. 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.
Am I right?
You are right.
-- Gustavo J. A. M. Carneiro INESC Porto, Telecommunications and Multimedia Unit "The universe is always one step beyond logic." -- Frank Herbert
participants (4)
-
Gustavo Carneiro -
Hrvoje Niksic -
pycnweb -
Stefan Behnel