Boost logo

Boost :

Subject: Re: [boost] enable_shared_from_this2
From: Peter Dimov (pdimov_at_[hidden])
Date: 2009-10-28 08:14:47


Berserker wrote:

> Thanks for yours replies, I hope that you'll find the time to have a
> look at my test :)
> Peter did you remember the problem related to boost::python I'm
> talking about? Here is the link of your reply
> http://lists.boost.org/boost-users/2008/08/38864.php

I remember now. But I thought I had fixed it. In this message I say:

> We've been thinking of changing the behavior of enable_shared_from_this in
> such cases, making it ignore subsequent shared_ptr instances to the same
> object and having it stick to the first one. This, however, will not
> happen for the upcoming 1.36 release of Boost.
>
> You can in principle achieve the same result by patching your local copy
> of Boost to make weak_ptr::_internal_assign only initialize when
> this->expired() is true, but this has never been tested.

and, in fact, the code in the trunk and in the release branch already does
this:

    // Note: invoked automatically by shared_ptr; do not call
    template<class X, class Y> void _internal_accept_owner( shared_ptr<X>
const * ppx, Y * py ) const
    {
        if( weak_this_.expired() )
        {
            weak_this_ = shared_ptr<T>( *ppx, py );
        }
    }

There is even a test for that, esft_second_ptr_test.cpp:

    boost::shared_ptr<X> px( new X );

    {
        boost::shared_ptr<X> px2( px.get(), null_deleter );
        BOOST_TEST( px == px2 );
    }

    try
    {
        boost::shared_ptr< X > qx = px->shared_from_this();

        BOOST_TEST( px == qx );
        BOOST_TEST( !( px < qx ) && !( qx < px ) );
    }
    catch( boost::bad_weak_ptr const& )
    {
        BOOST_ERROR( "px->shared_from_this() failed" );
    }


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk