[C++-sig] Re: Shared Pointers and Boost.Python

David Abrahams dave at boost-consulting.com
Mon Jul 26 02:56:50 CEST 2004

Ingo Lütkebohle <ingo at fargonauten.de> writes:

> Am Sat, 24 Jul 2004 10:39:50 -0400 schrieb David Abrahams:
>> You might want to use register_ptr_to_python<shared_ptr<X> >() for
>> some shared pointers if you ever create them from C++ code,
> Whats the difference between calling that method

It's a regular function template, not a method.

> and specifying a shared_ptr in the class_ call?  I noticed the two
> are mutually exclusive (getting a failed assertion at runtime
> otherwise).

The _warning_ (it should be a warning, not an assertion -- if it's an
assertion something is very wrong) is because both of them register a
converter from shared_ptr<X> to the Python class corresponding to X,
and Boost.Python warns whenever duplicate to-python converters are

Specifying a shared_ptr in the class_<...> argument list like

           class_<X,shared_ptr<X>, ...>

additionally means that when you create an instance of the Python
class corresponding to X, it holds the X instance via shared_ptr<X>.
The only upshot is that you can pass these Python X instances to a
wrapped C++ function that takes a shared_ptr<X> by non-const reference
(rather than by value), since there really is a persistent shared_ptr
that can be modified.  The shared_ptr<T> created when you call a
wrapped C++ function taking a shared_ptr<T> by value is normally
created on-the-fly for each function call.

> I'm commonly using factory methods where the smart ptr is instantiated on
> the C++ side.  Mostly using std::auto_ptr.  I once had problems using
> a boost::shared_ptr instead (hung on when calling 'del' from python). 
> Could that be a related problem?

Related?  I guess.  You haven't given much detail so it could be
almost anything I suppose.

Dave Abrahams
Boost Consulting

More information about the Cplusplus-sig mailing list