473,698 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python Memory Leak using SWIG

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_wrap per(x,self.nbs, self.nba,self.a c)

I then loop (which pretty much only calls this function) over and
over. I put the variable as a self.c hoping the garbage collector
would know how to delete it after the class goes out of scope. I also
tried explicitly deleting the variable (del var) in the loop with no
success. In all cases quiet a large memory leak occurs (and grows
rather quickly).

I believe this is comming from the fact that the thing that is
returned is a pointer to the data. So the returning object is a
pointer. The python garbage collector then doesn't know how to delete
this structure and probably (maybe) just deletes the pointer after the
class goes out of scope. Leave the data there and causing the memory
leak issue. I however doesn't know how to tell python that this
variable is a pointer and to delete whats going to it. Or perhaps
tell SWIG to delete the data, and return the structure some other way?

Here is the c++ wrapping code, perhaps there is an easy way to fix
this memory leak (I believe a lot of SWIG people probably do this)
perhaps some function call from the python? or some special call from
the SWIG? Thanks a bunch!

// set up the list output
PyListObject* out;
PyArrayObject* output;
out = (PyListObject*) PyList_New(0);
npy_intp dims[2];
int i,j;

for(i=0;i<g.siz e();i++)
{
// append a list for this scale
PyList_Append(( PyObject*) out,PyList_New( 0));

for(j=0;j<g[i].size();j++)
{
// set the dimensions for this scale & angle
dims[0] = g[i][j].n();
dims[1] = g[i][j].m();

// make an array for this scale & angle's data
output = (PyArrayObject* ) PyArray_SimpleN ewFromData(2, dims,
PyArray_CDOUBLE ,g[i][j]._data);
Py_INCREF((PyOb ject*) output);

// append this angle's data to the list for this scale
PyList_Append(P yList_GetItem(( PyObject*) out,i),(PyObjec t*)
output);

// zero the CpxNumMat for this scale & angle, hand ownership to
numpy
g[i][j]._data = NULL;
g[i][j]._m = 0;
g[i][j]._n = 0;
output->flags = output->flags | NPY_OWNDATA;
}
}

return (PyObject*) out;

Jun 4 '07 #1
1 3251
co*****@gmail.c om wrote:
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_wrap per(x,self.nbs, self.nba,self.a c)

I then loop (which pretty much only calls this function) over and
over. I put the variable as a self.c hoping the garbage collector
would know how to delete it after the class goes out of scope. I also
tried explicitly deleting the variable (del var) in the loop with no
success. In all cases quiet a large memory leak occurs (and grows
rather quickly).

I believe this is comming from the fact that the thing that is
returned is a pointer to the data. So the returning object is a
pointer. The python garbage collector then doesn't know how to delete
this structure and probably (maybe) just deletes the pointer after the
class goes out of scope. Leave the data there and causing the memory
leak issue. I however doesn't know how to tell python that this
variable is a pointer and to delete whats going to it. Or perhaps
tell SWIG to delete the data, and return the structure some other way?

Here is the c++ wrapping code, perhaps there is an easy way to fix
this memory leak (I believe a lot of SWIG people probably do this)
perhaps some function call from the python? or some special call from
the SWIG? Thanks a bunch!

// set up the list output
PyListObject* out;
PyArrayObject* output;
out = (PyListObject*) PyList_New(0);
npy_intp dims[2];
int i,j;

for(i=0;i<g.siz e();i++)
{
// append a list for this scale
PyList_Append(( PyObject*) out,PyList_New( 0));

for(j=0;j<g[i].size();j++)
{
// set the dimensions for this scale & angle
dims[0] = g[i][j].n();
dims[1] = g[i][j].m();

// make an array for this scale & angle's data
output = (PyArrayObject* ) PyArray_SimpleN ewFromData(2, dims,
PyArray_CDOUBLE ,g[i][j]._data);
Py_INCREF((PyOb ject*) output);

// append this angle's data to the list for this scale
PyList_Append(P yList_GetItem(( PyObject*) out,i),(PyObjec t*)
output);

// zero the CpxNumMat for this scale & angle, hand ownership to
numpy
g[i][j]._data = NULL;
g[i][j]._m = 0;
g[i][j]._n = 0;
output->flags = output->flags | NPY_OWNDATA;
}
}

return (PyObject*) out;
I think %newobject swig directive is a solution to your problem.

Pierre
Jun 5 '07 #2

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

Similar topics

5
2254
by: Gary | last post by:
Hi- I've been searching the web for a while and I've been unable to find a way to access c data objects in python without using SWIG. I can do methods just fine but I can't access variables. It seems like PyModule_AddObject should work but it segfaults my program. There's a good chance I'm just using it wrong, or I should be using something else. Could someone give me a simple, complete example pretty please? Thanks!
0
1842
by: Atul Kshirsagar | last post by:
I am embedding python in my C++ application. I am using Python *2.3.2* with a C++ extention DLL in multi-threaded environment. I am using SWIG-1.3.19 to generate C++ to Python interface. Now to explain it in details, 1. Python initialization and finalization is done in the *main* thread. 2. For each new thread I create a separate sub-interpreter . 3. Using PyRun_String("import myModule"...) before execution of python
13
3852
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some modifications along the way to make the interface more Pythonic. For example, all of these functions return an error code (typically just errno passed along, but not always). They all accept as one of their arguments a pointer to someplace to store...
2
4448
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>
0
995
by: Uwe Mayer | last post by:
Hi, I am wrapping a C function returning large amount of binary data back to Python using SWIG. I have the data malloc()ed or new()ed on the heap and buffer objects seem a good way to wrap it in python (http://docs.python.org/api/bufferObjects.html) From the documentation it seems PyBuffer_FromReadWriteObject() will not
18
2933
by: diffuser78 | last post by:
I have a python code which is running on a huge data set. After starting the program the computer becomes unstable and gets very diffucult to even open konsole to kill that process. What I am assuming is that I am running out of memory. What should I do to make sure that my code runs fine without becoming unstable. How should I address the memory leak problem if any ? I have a gig of RAM. Every help is appreciated.
0
361
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 378 open ( +3) / 3298 closed (+34) / 3676 total (+37) Bugs : 886 open (-24) / 5926 closed (+75) / 6812 total (+51) RFE : 224 open ( +7) / 227 closed ( +7) / 451 total (+14) New / Reopened Patches ______________________
0
1667
by: nejucomo | last post by:
Hi folks, Quick Synopsis: A test script demonstrates a memory leak when I use pythonic extensions of my builtin types, but if I use the builtin types themselves there is no memory leak. If you are interested in how builtin/pure-python inheritance interacts
113
5274
by: John Nagle | last post by:
The major complaint I have about Python is that the packages which connect it to other software components all seem to have serious problems. As long as you don't need to talk to anything outside the Python world, you're fine. But once you do, things go downhill. MySQLdb has version and platform compatibility problems. So does M2Crypto. The built-in SSL support is weak. Even basic sockets don't quite work right; the socket module...
0
8676
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9164
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...
0
9029
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7734
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...
0
5860
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
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3051
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.