Memory leak

Bernhard Herzog bh at intevation.de
Tue Mar 4 09:01:01 EST 2003


andrew.gregory at npl.co.uk (Andrew Gregory) writes:

> I keep being troubled by memory leaks when using C/C++ extensions (I
> have Python 2.2.2 on Windows). The problem is most acute in
> mathematical code, in which a C routine in an extension is called
> repeatedly in a loop. So I wrote a simple test program:
> 
> // test.h
> typedef PyObject* RESULTOBJ;
> 
>   // Hand-coded wrapper for returning string
>   RESULTOBJ wrap_string(char *s)
>   {
>     RESULTOBJ result;
>     result = PyString_FromString(s);
>     return Py_BuildValue("O",result);
>   };

Here's your memory leak. The "O" code will incref the argument. Use
either "N" or simply return result. In the latter case you might as well
get rid of wrap_string and use PyString_FromString directly.

   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/




More information about the Python-list mailing list