Python C Extensions

MRAB python at mrabarnett.plus.com
Thu Feb 24 11:33:45 EST 2011


On 24/02/2011 16:01, aken8062 at yahoo.com wrote:
> Hi,
>
> I have a memory leak problem with my "C" extension module. My C module
> returns large dictionaries to python, and the dictionaries never get
> deleted, so the memory for my program keeps growing.
>
> I do not know how to delete the dictionary object after it becomes
> irrelevant. I do not know if the version of python is relevant, I'm
> using the 2.5 !
>
>
> Here is the "C" code:
>
> PyObject *GetDictionary(PyObject *self, PyObject *args)
> {
>    PyObject *dict = PyDict_New();
>    PyObject *key;
>    PyObject *value;
>
>    char name[128];
>
>    for(int i = 0; i<  60; i++)
>      {
>        sprintf(name,"v%d",i);
>        float number = 1.0 * 0.5*i;
>
> PyDict_SetItem(dict,Py_BuildValue("s",name),Py_BuildValue("f",number));
>      }
>    return dict;
> }
>
> And here is the Code that I use in a loop, which causes the program
> memory to grow:
> import libpyTestModule as pyTEST
>
> bankTEST = {}
> for j in range(1,100000):
>      for k in range(1,100000):
>          bankTEST = pyTEST.GetDictionary()
>          del bankTEST
>
>
> Any help will be appreciated.

Py_BuildValue(...) returns an object with its refcount set to 1.

PyDict_SetItem(...) increments the refcounts of the key and value
objects when they are added to the dict, so their refcounts will then
be 2.

When the dict is garbage-collected the refcouts of the key and value
objects will be decremented to 1, so they won't be collected, and as
there aren't any other references to them, leading to a memory leak.

You therefore need to decrement the refcounts of the key and value
objects after adding them to the dict:

     PyObject *key = Py_BuildValue("s", name);
     PyObject *value = Py_BuildValue("f", number);
     PyDict_SetItem(dict, key, value);
     Py_DECREF(key);
     Py_DECREF(value);



More information about the Python-list mailing list