[C++-sig] differencein memory management

Gennadiy Rozental rogeeff at gmail.com
Thu Jul 17 20:20:24 CEST 2008


Alex Mohr <amohr <at> pixar.com> writes:

> 
> >>> 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, 

I never do this.

> 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.)

Yes. I've noticed this problem during development.
 
> 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.

I've got similar global registry maintained in my library that knows how convert 
my custom_ptr<A> to bp::object and back. I am using bp::object type as key in
one direction and unique A::TYPE (owr reflection system provides one) in other.

I never really thought about implementing it using boost.python to-value
conversion mechanism. It might be possible.

Gennadiy





More information about the Cplusplus-sig mailing list