[C++-sig] differencein memory management

Alex Mohr amohr at pixar.com
Thu Jul 17 18:46:06 CEST 2008


>>> this case I can't get from A* back to the python object, right?
>> Not unless you add some machinery to do so explicitly.
> 
> This is exactly what I did by storing bp:object inside A.

But then the problem is when you return a custom_ptr<A> to python, 
boost::python won't know about the python object stored inside A, so it 
creates a new python object, which will cause havoc for you later.  (You 
now have multiple python objects representing the same C++ object.)

One thing required to really solve this, in my experience, is to (quite 
grossly) replace the to-python conversion that boost.python creates when 
you say class_<...>() with one that attempts to find your associated 
python object first (in your case, inside the A instance) and if it's 
there, return it.  If it's not there, invoke the boost.python-generated 
to-python conversion to produce it, then store it inside the A instance 
for later.

Alex




More information about the Cplusplus-sig mailing list