From: David Abrahams (david.abrahams_at_[hidden])
Date: 2001-09-23 08:43:50
Yeah, you are missing something, though I have to admit it's a subtle point.
It took me quite a while to figure out what was going on with your case. The
problem is that because boost::shared_ptr<> uses a non-intrusive reference
count, you can't create a shared_ptr to any arbitrary object. Boost.Python
was designed to be able to handle objects returned from C++ held by all
kinds of smart pointers (e.g. std::auto_ptr). Given an arbitrary object of
type T, it isn't possible to safely create a shared_ptr<T>, because the T's
lifetime may be managed by some auto_ptr<> or even some other shared_ptr<>.
On the other hand, I think that it might be worthwhile to give special
status to shared_ptr<T> in Boost.Python. After all, auto_ptr<T> is
convertible to it, so we might consider managing all wrapped instances with
shared_ptr. I believe that approach would even be compatible with planned
extensions which allow returned references to subobjects (see
http://groups.yahoo.com/group/boost/message/10144). To do that, however,
would require some substantial rework of low-level code in the library.
Ralf Grosse-Kunstleve is maintaining a list of Boost.Python development
projects (http://cci.lbl.gov/~rwgk/boost.python/projects.html). If you are
interested in contributing a patch to the library, I'm sure it would be
----- Original Message -----
> I've just started looking at boost::python, and I am having a litte
> difficulty with functions that take shared_ptr's as arguments. I'm
> not sure if this is due to me misunderstanding how boost::python is
> supposed to work, or a bug somewhere in my code, or g++ or something
> else entirely.
> I created a class 'hello' based on the one in the getting_started2
> example, along with a free function 'invite' (again, as in the
> example). This worked fine. I then changed invite(const hello&) to
> invite(boost::shared_ptr<hello>). Now, when I try and call 'invite'
> from python, passing it a 'hello' instance created in python, I
> receive an error telling me that the extension class 'hello' does not
> wrap '<5hello>' (5hello, I discoverd, is what typeid(hello()).name()
> returns, for what that's worth).
> I looked for references to any similar problems in the documentation
> and in the archive for this list, but I didn't see anything about
> this specific issue.
> Have I misunderstood how boost::python interacts with smart pointers?
> invite(hello*) works the same as invite(hello&). Further, if I return
> a shared_ptr<hello> to python from a C++ function, this works just
> like a 'hello' instance in python, except that it _does_ work with
> shared_ptr version of 'invite'. This leads me to think that, from the
> python side, it is supposed to be transparent whether the C++
> function takes a T&, a T* or a shared_ptr<T>. Is this not the case?
> For what it's worth, I'm using boost 1.24.0, python 1.52 and g++
> 2.95.3; I don't know if one of these might be causing my
> difficulties, although I suspect the problem is of my own making.
> Tim Fisken
> 223 King's College
> 07740 928082
> Info: http://www.boost.org Unsubscribe:
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk