473,804 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2854

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
3789
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 = PyFloat_FromDouble($1);" %include mvf.h
0
1597
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 this function available in Python. I use SWIG but I'm a complete newbie with it. I've only found an example to convert a Python list to C char** http://www.swig.org/Doc1.3/Python.html#Python_nn59
2
4457
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 */ #include <time.h>
1
3266
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 and puts it in the PyArrayObject* which is returned as a PyObject*. I then call it from python like so: self.c = __f2.fdct2_wrapper(x,self.nbs,self.nba,self.ac)
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10359
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9177
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.