#686 OUPUT typemap --> python memory leak

python (259)

using the output typemap with Swig 1.3.24, Python 2.4
on Linux causes a memory leak.

For example, consider a swig declaration:

void func(double *OUTPUT, double *OUTPUT);

for the following C function:

void func(double *out1,double *out2) {

This function ca be used as expected:

>>> from swigmodule import func
>>> func()
(1324.0, 24.0)

However it produces a memory leak revealed by watching
memory usage with repeated iteration:

>>> while True:
... func()

-----> rapidly increased memory usage.

Please advise.


  • szager

    szager - 2011-04-25
    • status: open --> closed-wont-fix
  • szager

    szager - 2011-04-25

    I believe this is a characteristic of python, not SWIG. When I run this code:

    for i in range(10000000):
    i + 1

    ... I get the essentially the same memory profile as with your example.

  • S Joshua Swamidass

    Hello szager,

    This is not a problem with python. Your code is not memory efficient. A better version:

    for i in xrange(10000000): i + 1

    Range generates the full list first in memory before performing the iteration. Xrange does not and uses constant memory.

    This is clearly a bug in the swig template. The workaround is to add in code to free out1 and out2 our avoid the template altogether. But this is clearly a memory leak error in swig's template.

  • S Joshua Swamidass

    One more quick point. If you swig the code I put up there, and run

    while True: func()

    Then this will increase memory to the point of causing a seg fault.

  • szager

    szager - 2011-04-26

    OK, I'm reopening this bug, but I need some more info from you. You mention a typemap, but you don't have any in your example. I assumed that you were using these typemaps from the SWIG python documentation:

    %typemap(argout) double * {
    PyObject *o, *o2, *o3;
    o = PyFloat_FromDouble(*$1);
    if ((!$result) || ($result == Py_None)) {
    $result = o;
    } else {
    if (!PyTuple_Check($result)) {
    PyObject *o2 = $result;
    $result = PyTuple_New(1);
    o3 = PyTuple_New(1);
    o2 = $result;
    $result = PySequence_Concat(o2,o3);

    %typemap(in,numinputs=0) double * (double temp) {
    $1 = &temp;

    Using those typemaps with the SVN-latest SWIG and python2.7, I can't reproduce your problem. I run with the 'while True' loop, and the process memory usage stays flat.

    Can you confirm that you're using the above typemaps? If so, can you try with a more recent version of SWIG and see if it helps?


  • szager

    szager - 2011-04-26
    • assigned_to: marcelomatus --> szager
    • status: closed-wont-fix --> open-accepted
  • szager

    szager - 2011-04-26

    I should clarify that the typemap in my previous comment is also the default OUTPUT typemap.

  • S Joshua Swamidass

    Oh I see there is a new version. Let me try it out and get back to you.

  • S Joshua Swamidass

    So, I think you can safely close this bug. It seems to be fixed in the current swig version. That's godo news. Sorry about the bother, this bug report is from about 5 years ago.

  • szager

    szager - 2011-04-27
    • status: open-accepted --> closed-fixed
  • szager

    szager - 2011-04-27

    No problem; I'm glad it's fixed, and I learned something new about python (range vs. xrange).


Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks