473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

SWIG typemap leading to a memory leak


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.

Jul 18 '05 #1
2 2840

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

Jul 18 '05 #2
Skip Montanaro wrote:
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.

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ernie | last post by:
Hi, I am learning swig to use C codes from Python. Here is the swig file mvf.i: %module mvf %include "carrays.i" %array_class(double, doubleArray); %typemap(out) double, float "$result =...
0
by: Chris | last post by:
Hi I have a C function that builds a list of null terminated strings: void buildStringList(char **asStrings, int n); The number of elements in the list is given in input. I'd like to have...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
1
by: cody314 | last post by:
Versions: Python 2.5.1 (r251:54863, May 14 2007, 10:50:04) SWIG Version 1.3.20 Hello I have some code that wraps a C++ library so I may use it in python. The code basically just gets some data...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.