Question about PyDict_SetItemString

Stefan Behnel stefan.behnel-n05pAM at web.de
Fri Jul 13 05:01:49 EDT 2007


lgx schrieb:
> Does PyDict_SetItemString(pDict,"key",PyString_FromString("value"))
> cause memory leak?
> 
>>From Google results, I find some source code write like that. But some
> code write like below:
> 
> obj =  PyString_FromString("value");
> PyDict_SetItemString(pDict,"key",obj);
> Py_DECREF(obj);

Sorry, my previous response came from the completely wrong bag.

The second is right. PyString_FromString() increases the refcount of the
returned string, so you have to decref it after use.

PyDict_SetItemString() does its own refcount increase internally when it
stores the value, so don't bother about it at all when you think about what
refcounts you have to increase or decrease.

Stefan



More information about the Python-list mailing list