Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#686 OUPUT typemap --> python memory leak

closed-fixed
szager
python (258)
5
2011-04-27
2006-06-05
No

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) {
*out1=1324;
*out2=24;
}

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.

Discussion

1 2 > >> (Page 1 of 2)
  • 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.

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

     
  • 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);
    PyTuple_SetItem(target,0,o2);
    }
    o3 = PyTuple_New(1);
    PyTuple_SetItem(o3,0,o);
    o2 = $result;
    $result = PySequence_Concat(o2,o3);
    Py_DECREF(o2);
    Py_DECREF(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?

    Stefan

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

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

     
  • 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
     
1 2 > >> (Page 1 of 2)