I have a huge memory leak problem related to the C extension I am developing. In C, I have an array of doubles called A , and an int variable called AnotherIntVariable , which I want to pass to Python. Well, in my C extension module, I do the following:
int i; PyObject *lst = PyList_New(len_A); PyObject *num; if(!lst) return NULL; for(i=0;i<len_A;i++){ num=PyFloat_FromDouble(A[i]); if(!num){ Py_DECREF(lst); return NuLL; } PyList_SET_ITEM(lst,i,num); } free(A); return Py_BuildValue("Oi",lst,AnotherIntVariable)
So, in Python, I get this list and int as follows:
Pyt_A,Pyt_int=MyCModule.MyCFunction(...)
Where Pyt_A and Pyt_int are the list and integer obtained from my C-extension " MyCModule ", from the function " MyCFunction ", which I described earlier.
The problem is that in Python I use this Pyt_A array (so I use Py_BuildValue instead of a simple return to make INCREF to save this variable momentarily from the garbage collector), but then I need to dereference it somehow to free allocated memory. The problem is that I use the MyCFunction function several times, and this causes a memory leak, because I do not know how to dereference the array I get in python to get rid of it.
I tried just returning the array by doing return lst in the C part of the code instead of Py_BuildValue("Oi",lst,AnotherIntVariable) , but this only leads to a segmentation error when I try to use it in python (possibly because the garbage collector did his work) ...
... what am I missing here? Can anybody help me?