Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-12-18 12:27:55


Rani Sharoni wrote:
>
> It still bothers me that it's easy to write code like the following:
> ~A::A()
> {
> shared_ptr<T> p(weak_ptr_);
> p->cleanup();
> }

It's even easier to write

~A::A()
{
    p->cleanup();
}

in cases where the correct form would have been

~A::A()
{
    if(p) p->cleanup();
}

The situation above is similar, it should have been

~A::A()
{
    if( shared_ptr<T> p = weak_ptr_.lock() )
    {
        p->cleanup();
    }
}

The original code is broken, no matter whether the constructor throws an
exception on expired() or not. I'm not sure whether the revised form isn't
broken too, from a design point of view, but let's put that aside. ;-)


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