SWIG typemap leading to a memory leak

Istvan Albert ialbert at mailblocks.com
Mon Aug 18 16:13:10 EDT 2003


I'm wrapping a C++ library with SWIG. Everything
worked fine and dandy until I let to system run for
longer periods of time. At that point I  noticed that
my scripts use up a whole lot of memory.

I tried to locate the problem, and I ended up with an
example that is very simple yet exhibits the same
behavior. The SWIG wrapper has the following:

typemap(out) int {
   int key;
   $result = PyDict_New();	
   for(key=0; key < $1; key++) {
     PyDict_SetItem($result, PyInt_FromLong(key), PyInt_FromLong(key));
   }
}

this wraps a function that returns an integer and
creates and returns a dictionary containing keys and values from
0 to the integer in question. When I call the function
as below:

for val1 in range(1000):
	for val2 in range(300):
		g.test(val2)

it uses up all the memory in the system. (The test method
belongs to a C++ class and simply returns its argument).
The only answer I could come up with is that  Python does not
garbage collect the dictinary created in the wrapper file,
and that fills up the memory. Does anyone know what
is going on? Thanks.

Istvan.





More information about the Python-list mailing list