Problems with SWIG and nested structure pointers

bgkloss at my-deja.com bgkloss at my-deja.com
Wed Dec 1 09:38:23 EST 1999


Hi - I've been using SWIG with Python quite successfully, but one
problem remains: Memory allocation with pointers to structures.

I need to wrap structures of the form:

struct A
{
   int x;
};

struct B
{
   struct A* a;
   int y;
};

after setting up SWIG constructors and destructors I can write (in
python)

a = A()
b = B()
b.a = a
a = None # - but this calls the destructor

The last line is problematic, since Python has just lost the reference
count to the A object, while I still have a pointer to it in my B
structure.  For a quick and dirty wrap I probably wouldn't care, but in
this case it's essential that I avoid memory leaks in all circumstances
- preferrably without imposing too much overhead on the python
programmer.

Any suggestions?


Sent via Deja.com http://www.deja.com/
Before you buy.




More information about the Python-list mailing list