[C++-sig] Re: Python + Boost Python V2 + downcasting

Nicolas Lelong n_lelong at hotmail.com
Mon Nov 11 17:53:07 CET 2002

David Abrahams wrote:

> [digression:
> I notice that you're using return_internal_reference<>() to return the
> Base* from the ObjectServer. Does the ObjectServer actually control
> the lifetime of the objects, or is it simply holding pointers? I ask
> because I don't see a destructor, so in the code you sent,
> return_internal_reference<> is inappropriate (maybe you were just
> eliminating details for the sake of the post?) -- the point of
> return_internal_reference<> is to keep the owning object alive as long
> as the owned objects are also alive. If something else is managing
> their lifetimes, you may need another mechanism.
> ]

nope, of course the ObjectServer manages the lifetime of the Base objects ;
I just made up a simple example for the post.

> ...
> In the meantime, if the ObjectServer is really the owner of the C++
> objects, you might consider storing boost::python::object in its
> m_objects array. If you convert your Derived objects to
> boost::python::object at their point of creation, the full dynamic
> type will be preserved for Python.

I'll use some other mecanisms for now (such as a collection of functions
that wrap a collection of dynamic_casts - as for my tests I don't need a
full blown solution), as I try to extend Python with existing code and I
like the non-intrusive paradigm.

As for your other points, I'll try to take some time to take a closer look
to all this ... I'll see what I can get playing with custom return value
policies and/or boost/python/object/make_instance.hpp [I'll have a look at
this first, I'd really like to have a 'working' isinstance :) ].

I'll let you know if I have some results !

Thanks for the hints & your help,

More information about the Cplusplus-sig mailing list