Opened 10 years ago

Closed 10 years ago

#2584 closed Bugs (fixed)

boost::enable_shared_from_this + boost.python library

Reported by: Nicolas Lelong <nico_ml@…> Owned by: Peter Dimov
Milestone: Boost 1.39.0 Component: smart_ptr
Version: Boost 1.37.0 Severity: Problem
Keywords: enable_shared_from_this Cc:


A problem that was raised, on february, on c++-sig mailing list about a problem happening when wrapping classes derived from 'enable_shared_from_this' using boost.python library.

The problem is briefly described in this post - including a patch to boost/shared_ptr.hpp that would allow to work around the problem.

To make it short, boost.python creates shared_ptr objects, holding the wrapped c++ objects, with a custom deleter managing the python object reference count. This leads to something like that :

#include <boost/enable_shared_from_this.hpp>

namespace {

class A : public boost::enable_shared_from_this<A>

~A() {};

void my_deleter(void*)

BOOST_AUTO_TEST_CASE( test_enable_shared_from_this )

boost::shared_ptr<A> a( new A );

boost::shared_ptr<A> ater =

boost::shared_ptr<A>( a.get(), my_deleter );

OP patch proposal:
boost::shared_ptr<A> ater =
boost::shared_ptr<A>( a.get(), my_deleter
, boost::dont_enable_shared_from_this() );
BOOST_CHECK( a == ater );

boost::shared_ptr<A> abug = a->shared_from_this(); this throws bad_weak_ptr


Change History (6)

comment:1 Changed 10 years ago by trotterdylan@…

This is a fairly major issue for my Boost.Python project. I'm currently using a patched 1.37 distribution, but I'd like to go back to a standard distribution for 1.39. Is this issue on the radar for that release?

comment:2 Changed 10 years ago by nico_ml@…

I don't know if you're aware of a patch that I've submitted to boost.python, that uses shared_ptr aliasing constructor to fix the problem.

The patch has been integrated in boost.python svn trunk, but I doubt it has been released in boost 1.38.

comment:3 Changed 10 years ago by trotterdylan@…

Ah! I'm glad to hear it's in the trunk. I have integrated the patch into my 1.37 local installation, but ultimately I'd like to get back to using standard installs. Do you think the patch will be included in 1.39?



comment:4 Changed 10 years ago by Peter Dimov

Milestone: Boost 1.38.0Boost 1.39.0
Status: newassigned

comment:5 Changed 10 years ago by Peter Dimov

(In [51581]) Fix enable_shared_from_this-related tickets in trunk. Refs #2126. Refs #2584.

comment:6 Changed 10 years ago by Peter Dimov

Resolution: fixed
Status: assignedclosed

(In [51632]) Merge [51581] to release. Fixes #2126. Fixes #2584.

Note: See TracTickets for help on using tickets.