Boost logo

Boost :

From: George A. Heintzelman (georgeh_at_[hidden])
Date: 2002-05-03 17:37:41


> > From: "David B. Held" <dheld_at_[hidden]>
> > > > > Also, take note of the situations in which using a
> > > > > custom deleter is dangerous.

[snip]

> 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?

A compile error?

At least, that's what happens to me, even after I fix the casting to/from void *:

caiman:~/test> g++ -I$HOME/boost-new test_shared.cpp
test_shared.cpp: In function `class boost::shared_ptr<int> create()':
test_shared.cpp:14: no matching function for call to `boost::shared_ptr<int>::shared_ptr (boost::shared_ptr<int> &, void (&)(int *))'

The reason is, the delete function is associated with the reference count in the initial call to the shared pointer, and hence passed around. You have to count on the initial creation of the shared_ptr getting it right, of course, but I don't think there's anything you can do about that.

I think you're thinking shared_ptr does something it doesn't...

George Heintzelman
georgeh_at_[hidden]


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