Boost logo

Boost Users :

From: perkristensennettest (per.kristensen_at_[hidden])
Date: 2002-11-08 09:34:08


To me it seems like a bug, that the weak_count in
counted_base::release is decremented before the call to dispose. See
below

//source code begin
    void release() // nothrow
    {
        long new_use_count;
        long new_weak_count;

        {
#ifdef BOOST_HAS_THREADS
            mutex_type::scoped_lock lock(mtx_);
#endif
            new_use_count = --use_count_;
            new_weak_count = --weak_count_; //this is a bug
        }

        if(new_use_count == 0)
        {
            dispose(); //the this object may be deleted here
        }

        if(new_weak_count == 0)
        {
            // not a direct 'delete this', because the inlined
            // release() may use a different heap manager
            self_deleter_(this);
        }
    }
//source code end

If the call to dispose() indirectly calls weak_release there is an
error as weak_release will decrement the --weak_count and may
destruct the counted_base object. This will happen if you have an
class C with a member of type weak_ptr<C>.

As I see it the decrement of weak_count should be moved to after the
dispose call. see below:

//source code begin
    void release() // nothrow
    {
        long new_use_count;
        long new_weak_count;

        {
#ifdef BOOST_HAS_THREADS
            mutex_type::scoped_lock lock(mtx_);
#endif
            new_use_count = --use_count_;
            //do not decrement weak_count here

        }

        if(new_use_count == 0)
        {
            dispose();
        }

        {
#ifdef BOOST_HAS_THREADS
            mutex_type::scoped_lock lock(mtx_);
#endif
            new_weak_count = --weak_count_;
        }

        if(new_weak_count == 0)
        {
            // not a direct 'delete this', because the inlined
            // release() may use a different heap manager
            self_deleter_(this);
        }
    }
//source code end

????


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net