5

After writing up a wrapper in SWIG for my C++ algorithms i constantly get this error when I quit the Python interpreter after importing the module:

    $ python
    iPython 2.5.6 (r256:88840, Mar 10 2012, 14:05:15) 
    [GCC 4.4.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>>from algol import *
    >>> 
    *** glibc detected *** python: double free or corruption (!prev): 0x0000000001e42430 ***

Then I have to Ctrl+C to get back control... what is happening?

I am running the following commands to build up my SWIG wrappers:

$NAME=algol
swig -c++ -python $NAME.i
g++ -fpic -c $NAME.cpp $NAME.hpp $NAME\_wrap.cxx -I/usr/local/include/python2.5
g++ -Xlinker -zmuldefs -shared $NAME.o $NAME\_wrap.o -o _$NAME.so

My swig interface file is just an include of algol.hpp:

%module algol
%{
#include "algol.hpp"
%}
%include "algol.hpp"

What do you think about this? :S

Edit: attached sample source code here -> http://pastebin.com/q210vEAs

Cristian
  • 115
  • 1
  • 9
  • You probably have a memory leak in your C++ algorithm somewhere. A tool like [Valgrind](http://www.valgrind.org) might help you find it. – Ken May 12 '12 at 03:53
  • @Ken - doesn't sounds like a leak at all. Leak's don't cause crashes with warnings about freeing things twice! Valgrind can still help with double frees though. – Flexo May 12 '12 at 08:53
  • More likely you have violated the [rule of three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) somewhere and SWIG has generated code that uses your classes in a different way to how you'd tested them. (If you do a shallow copy somewhere in a type that class `free()` or `delete` in its destructor then that would cause exactly this scenario). Without seeing more code it's *impossible* to do more than speculate though. Can you make a *minimal* but complete example that illustrates your problem? (You might even find the problem yourself in doing so!) – Flexo May 12 '12 at 08:56
  • First of all, sorry if I haven't attached any source code, I didn't want it to be a huge pure-code question! @awoodland I am using a library for image processing called Olena, more specifically the Milena package. Here are the source code my algol.cpp and hpp. Let me tell you before hand that I don't perform any malloc/realloc...etc operation: http://pastebin.com/q210vEAs – Cristian May 12 '12 at 10:40
  • Have the problems been solved? – FreeToGo Apr 21 '14 at 20:48

1 Answers1

1

what is happening?

Exactly what the message says: either some code performed a double-free, or some other heap corruption.

As suggested by awoodland, run python under Valgrind, and see where that corruption or double-free is happening.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • After running several times, I found out this: swig/python detected a memory leak of type 'mln::image2d< mln::value::int_u8 > *', no destructor found. I think I have to implemente a destructor, but I am not sure if this is what I have to do or how to do so.. – Cristian May 13 '12 at 00:05
  • 1
    @kersian No: memory leak has *nothing* to do with the problem. Update your question with entire valgrind output, and we may be able to help you more. – Employed Russian May 13 '12 at 00:21