From: Eric Wing <ewmailing@gm...> - 2013-08-26 20:00:12
I am having problems with memory life-cycles (in garbage collected
languages) and equality for this specific kind of C pattern.
memcpy kind of represents the pattern I have:
void* memcpy(void *restrict s1, const void *restrict s2, size_t n);
Basically, I pass in a pointer for s1, and I expect to get that same
pointer back through the return value.
The problem is that every time SWIG returns my C pointer, it creates a
new proxy object around the pointer which is different than the
original proxy wrapper. This is breaking both equality tests and also
causing subtle memory management problems.
object1 = mymodule.CreateMyObject(); // uses %newobject and annotation
to call FreeMyObject when collected
object2 = mymodule.DoSomethingWithAndReturnObject(object1);
if(object1 != object2)
print("Drat: I want the objects to be the same, but they are not");
object1 = null;
mymodule.DoSomethingElseWithObject(object2); // will crash if the
garbage collector kicked in to clean object1
So object1 != object2 in my case even though the underlying C object
is the same. The problem is the SWIG proxy wrappers are different.
And then the next problem is that when object1 is collected, it calls
FreeMyObject on my C underlying C object. The expectation was that
object2 was still holding a reference to the same object so this
should not be collected. But in fact, this is not the case, so object2
is now hanging on to a stale pointer and will cause problems.
Is there a general way to handle this situation with SWIG?
Beginning iPhone Games Development
Get latest updates about Open Source Projects, Conferences and News.