Boost logo

Boost :

From: Darin Adler (darin_at_[hidden])
Date: 2002-05-04 10:55:24


On Friday, May 3, 2002, at 03:20 PM, David B. Held wrote:

>>> shared_ptr<FILE> f(p, fclose);
>>> shared_ptr<FILE> g(0, fflush);
>>>
>>> g = f;
>>>
>>> Contrived, but compilable.
>
> In this case, maybe. But take the more serious case of Win32
> handles with the wrong deallocator specified. Or maybe this is
> a better example to illustrate the problem:
>
> void myfree(void* p)
> {
> delete p;
> }
>
> shared_ptr<int> create()
> {
> shared_ptr<int> f(malloc(sizeof(int)), free);
>
> return shared_ptr<int>(f, myfree);
> }
>
> Guess what happens now?

I think you are misunderstanding the deleter feature of shared_ptr. The deleter is associated with the pointer it's passed in with at either construction time or reset time. It won't ever be used on another pointer.

     -- Darin


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