Boost logo

Boost :

Subject: Re: [boost] [shared_ptr] calling functions by reference
From: Matt Calabrese (rivorus_at_[hidden])
Date: 2011-05-29 00:32:28

On Sun, May 29, 2011 at 12:18 AM, Sid Sacek <ssacek_at_[hidden]>wrote:
> I agree with you that your example above will crash the program, but
> calling Reset() is micro-managing the smart-pointer. There should be no good
> reason to call reset on a regular basis, and only have objects go out of
> scope. That is my understanding of the point of the shared_ptr<>.

It's perfectly fine and correct to call reset and I would hardly call it
micro-managing. In fact, I'd call it good practice to reset. You should
generally control your resource cleanup and not leave dangling smart
pointers around when you are done with whatever they point to. Immediate,
deterministic disposal is often very important.

> Let's assume that reset is never used, and shared_ptr<> is not set to a
> global variable, then the only thing that comes to my mind is that there
> might be a multi-threading subtlety that I'm not aware of.

The example I used only had it as a global for simplicity. The same
situation can and does come up without any globals at all in a
single-threaded environment and even when reset is not used (the pointer can
simply be assigned to point somewhere else and the issue I showed still

-Matt Calabrese

Boost list run by bdawes at, gregod at, cpdaniel at, john at