SWIG typemap leading to a memory leak

I

Istvan Albert

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

Skip Montanaro

You need to DECREF the result of PyInt_FromLong(key):

typemap(out) int {
int key;
PyObject *obj;
$result = PyDict_New();
for(key=0; key < $1; key++) {
obj = PyInt_FromLong(key);
/* obj has a refcount of 1 */
PyDict_SetItem($result, obj, obj);
/* obj has a refcount of 3 */
Py_DECREF(obj);
/* obj has a refcount of 2 because of its use in the dictionary */
}
}

PyDict_SetItem will bump the reference counts of the objects you pass into
it. When the dictionary goes away, it does its DECREFs, but all those
integers still have non-zero reference counts as a result of their initial
creation by PyInt_FromLong. You have to take care of that.

Skip
 
I

Istvan Albert

Skip said:
You need to DECREF the result of PyInt_FromLong(key):
PyDict_SetItem will bump the reference counts of the objects you pass into
it. When the dictionary goes away, it does its DECREFs, but all those
integers still have non-zero reference counts as a result of their initial
creation by PyInt_FromLong. You have to take care of that.

Fixed my problem right away.

Thanks a bunch!

Istvan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top