12 Aug
2009
12 Aug
'09
4:29 p.m.
Hello, I have a working application which extends a C api to python and everything works as I wanted to. I'm now in a cleaning phase where I want to improve my stuff, especially to avoid memory leak.
Here is a simple example of what seems correct to me but for which I would like an expert opinion :
PyObject * test() { PyObject *dict = PyDict_New(); // ... some code to initialize dict
PyObject *item = PyDict_GetItemString(dict, "key");
Py_INCREF(item);
Py_DECREF(dict);
return item;
}
As far as I understood, PyDict_GetItemString() returns a borrowed reference to the element so I need to do Py_INCREF(item) to own this reference. But is this needed ? If I only do return item, will the received item be owned by the caller of the test method ?
Thanks, Jerome