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

Daniel Jiménez dani.berzas at gmail.com
Wed Oct 5 04:19:08 CDT 2005


Hi ,again:
only say that the problem is solved, the answer was on the manual (RTFM,
me culpa).
http://docs.python.org/api/memoryOverview.html

Python and C uses different heaps. Python can't free memory allocated
with malloc(), alloc(), etc...
---------------------------------------------------------- 
wrong --> hp = (double *) calloc(tama_hp ,sizeof(double));
right --> hp = (double *) PyMem_New (double, tama_hp);
----------------------------------------------------------
Sorry and thanks.
Dani.


> 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 .
> 



More information about the SciPy-user mailing list