From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-05-15 06:21:40
From: "Thomas Witt" <witt_at_[hidden]>
> On Tuesday 14 May 2002 20:02, Peter Dimov wrote:
> > This will no longer be necessary with 1.28.0; we've removed operator*
> > operator-> from weak_ptr as unsafe. Now the only way to do something
> > weak_ptr is to construct a shared_ptr first, via its constructor or
> > make_shared.
> can you explain why make_shared adds safety. Currently I am unable to see
> IIUC what happens is a NULL ptr is dereferenced with or without
> On the first look it seems as if make_shared pretends to be safe but
> When make shared is used the object might be gone already.
Consider this example:
if(wp.get() != 0)
Looks innocent enough, but wp->f() may invalidate wp (by resetting the
shared_ptr holding the object), and wp->g() will crash.
Compare with the idiomatic make_shared approach:
if(shared_ptr<T> sp = make_shared(wp))
Now you have another shared_ptr, sp, keeping the object alive, preventing
In a multithreaded program, even wp->f() can crash; between the check and
the invocation, another thread may have destroyed the object.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk