Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-10-23 08:52:00


"Maxim Egorushkin" <e-maxim_at_[hidden]> writes:

> Michael Grundberg wrote:
>
> MG> In other words, we do:
>
> MG> // Our resource int i;
>
> MG> // Give it to the library boost::shared_ptr<int> p1(&i,
> MG> null_deleter()); std::set<boost::shared_ptr<int> > s;
> MG> s.insert(p1);
>
> MG> // A bit later boost::shared_ptr<int> p2(&i, null_deleter());
> MG> s.erase(p2);
>
> MG> Which doesn't remove the pointer from s. This is causing us alot
> MG> of bugs, since the compilations succeed but the code is broken.
>
> Looks like you are misusing shared_ptr<>.
>
> The key point is that shared_ptr<> is an exclusive owner of a raw
> pointer. No more then one shared_ptr<> should be constructed using the
> same raw pointer (unless it is 0 pointer). Violating the rule leads to
> multiple releases on the raw pointer. Using null_deleter allows you to
> remain unpunished :).

I'm not sure that this should be classified as an abuse. It seems a
little unclear that this side effect of allowing custom deleters was
anticipated. There are other ways to induce the same problem which
don't involve null_deleter.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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