[SciPy-user] memory leak with Python/C API

Robert Kern rkern at ucsd.edu
Mon Oct 3 06:56:43 CDT 2005

Daniel Jiménez wrote:
> Hi, every one,
> First sorry if this is not the right the place to ask this question.

Probably not. You'll probably get a better response at a more general
forum like comp.lang.python .

> I'm having memory leak when working with Python/C API.
> A big amount of data is generated in C and transfered to Python, but
> after use the memory is not freed. The program run until the OS kill the
> process.
> I'm working with Ubuntu-Warty, Python 2.3.4,
> [GCC 3.3.4 (Debian 1:3.3.4-9ubuntu5)] on linux2
> with scipy 0.3.2
> The C program goes as follows:
> {
> Load the initial data (list of arrays) with PyArg_ParseTuple().
> ExitList = PyList_New(..);
> HpList = PyList_New(n_esp);
> loop_1
>   { hp = calloc()
>     (compute hp)
>     PyList_SET_ITEM(HpList,i,...(char *)hp) );

You're eliding a lot of the code that's going to tell us where your leak
is. For example, PyList_SET_ITEM doesn't take char* as an argument. Do
you have a PyString_FromString() in there? It's entirely possible that
the function you are calling copies the data in the char*.
PyString_FromString() certainly does. You will have to free(hp) yourself.

>   }
> PyList_SET_ITEM(ExitList,0,(PyObject *)HpList);
> return ExitList;
> }

It would help if you could boil down your function to the smallest
complete example that displays the problem. In the process, you'll
probably find the answer yourself. If you don't, I'll be waiting on
comp.lang.python .

Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter

More information about the SciPy-user mailing list