Boost logo

Boost :

From: John Panzer (jpanzer_at_[hidden])
Date: 2000-03-08 21:25:52


Miki Jovanovic wrote:

> "marco manfredini" <marc-_at_[hidden]> wrote:
> > void doit()
> > {
> > w->work();
> > }
> ...
> Hi Marco,
>
> Complicated e-mail, and complicated code, for a simple question. All
> smart pointers out there do provide a 'locking' mechanism as you call
> it. And that is a temporary pointer, which you have actually mentioned.
>
> ...
> So your code should simply become:
>
> void doit()
> {
> _ptr<Worker> temp( w );
> temp->work();
> }
>

Leaving aside the issues in the rest of this thread, I like this idiom
fine. However, if you have a bare pointer in the first place (such as
"this"), a non-intrusive smart pointer is dangerous:

void Worker::doit()
{
    _ptr<Worker> temp(this); // Assumes no other smart ptrs exist...
    work(); // Don't need to use smart ptr, it just has to exist.
}

(The problem: If there is actually another non-intrusive smart pointer
pointing at "*this", then you end up with two independent reference counts
and a double deletion.)

This is a recurring worry of mine wrt non-intrusive smart pointers; can
anyone allay my fears about this? Is there any way to either

(1) Disallow this, or
(2) Allow it and make it safe?

Or (3) do people not feel that this is a real issue?

Thanks,
John Panzer




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