Boost logo

Boost :

Subject: Re: [boost] [shared_ptr] calling functions by reference
From: Sid Sacek (ssacek_at_[hidden])
Date: 2011-05-29 00:51:44

-----Original Message-----
From: boost-bounces_at_[hidden] [mailto:boost-bounces_at_[hidden]] On Behalf Of Lars Viklund
Sent: Sunday, May 29, 2011 12:10 AM
To: boost_at_[hidden]
Subject: Re: [boost] [shared_ptr] calling functions by reference

> Concurrent access to different shared_ptr instances in the same family
> (sourced from the same source) is safe.
> Concurrent access to a single shared_ptr instance is generally unsafe.

You are confirming my suspicion about multi-threading issues.

> Passing the shared_ptr by reference puts the onus on you to ensure that
> the shared_ptr instance you pass in is valid for the duration of the
> function call, which is non-trivial in general.

I understand. But doesn't it help to have guidelines and standards when it comes to working with shared_ptr<>?

> Examples of the above would be foo(some_global_ptr); or foo(bar->baz);
> where some_global_ptr is destroyed somewhere deep in the call chain, or
> bar is destroyed during the call sequence.

If I understand you correctly, somebody performed a reset() on something along the call sequence. Would I be correct in saying that if reset() isn't used in a busy call sequence, then this problem wouldn't arise? Wouldn't using a const-reference, like you mention below, identify potential problems like that?

> I've had real-life bugs of this kind. It's not pretty. Unless you have a
> genuine reason to access the same instance, try to avoid passing it by
> reference-to-non-const. If you feel you need to avoid the non-free
> copying cost, pass it by reference-to-const.
> --
> Lars Viklund | zao_at_[hidden]

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