Edited 12 Feb
I've just recently come up with an odd crash using some SWIG-generated Python wrappers for some C++ classes. It seems that the combination of SWIG and Python together are somewhat eager to clean up temporary values. So eager, in fact, that they're cleaned up while they're still being used. A significantly condensed version looks like this:
/* Example.hpp */
struct Foo {
int value;
~Foo();
};
struct Bar {
Foo theFoo;
Bar();
};
/* Example.cpp */
#include "Example.hpp"
Bar::Bar() {theFoo.value=1;}
Foo::~Foo() {value=0;}
/* Example.i */
%module Example
%{
#include "Example.hpp"
%}
%include "Example.hpp"
I run SWIG (1.3.37) on the .i file, and then in Python, have:
Python 2.4.3 (#1, Sept 17 2008, 16:07:08)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-41)] on linux2
Type "help", "copyright", "credits", or "license" for more information.
>>> from Example import Bar
>>> b=Bar()
>>> print b.theFoo.value # expect '1', since Bar's constructor sets this
1
>>> print Bar().theFoo.value # expect '1', since we're still using the Foo object
26403424
It seems that in the second instance, the temporary Bar object is destroyed before we ever get to read theFoo's value field. Chasing things around in gdb, this is clearly what's happening. And so by the time we read .value from Bar().theFoo, C++ has already destroyed (and overwritten with some other heap allocation) .theFoo. In my actual situation, this is causing a segfault.
Is there any SWIG directive or trick that I can add to my Example.i file to make Bar().theFoo.value return 1 here?
Second Update:
Well we know that the basic problem is that python destroys
Barimmediately. WhenBaris implemented in python, python's gc knows that there's still a reference totheFoo, and so does not destroy it. But whenBaris implemented in c++, python calls the c++ destructor, which automatically destroystheFooalong withBar.So the obvious solution is to prevent python from destroying
Barprematurely. Here's a slightly hackish solution involving subclassingBar:This saves a reference to the last
Barcreated so that it isn't destroyed right away. When a newBaris created, the old one is deleted. (My old version was silly; no need to override__del__for this.) Here's the output (withcout << "deleting Foo "inFoo's destructor):I still don't love this. It might be better to sequester the "persistent" behavior in a decorator; I tried that too and it worked (if you want to see the code let me know). It would definitely be better to somehow tell python to handle destroying
theFooitself, but I can't figure out how to do that.First Update:
The wrap code told me nothing, so I looked in swigexample.py. That also yielded nothing. Things became clearer when I tried duplicating
Barin pure python:Now we import Bar from pyfoobar:
This behavior is coming from Python!
Original Answer:
It seems like there's definitely some garbage collection combat at work here... Here's some related information on SWIG Memory Management. Based on this, it looks like the %newobject directive might be what you're looking for; but I tried several variations and couldn't get it to give python control over
theFoo:I'm beginning to suspect that this is intentional; seems like this line from the above link is relevant here:
But I'm not certain. I'm going to look at the swigexample_wrap code to see if I can figure out when
~Baris being called.