Boost logo

Boost :

From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2002-03-21 15:52:36


----- Original Message -----
From: "E. Gladyshev" <egladysh_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Thursday, March 21, 2002 5:05 PM
Subject: RE: RE: [boost] shared_prt, will it crash?

> Nice solution, but it is not the point here
> there are a lot of other solutions as well.
>
> The point is that the fool() is fool and
> he doesn't want to know how to make or follow
> any memory mananagement/ownership contracts :).
>
If fool() doesn't want to know about ownership contracts why is it using
shared_ptr<>?
A smart_ptr is precisely about ownership contracts, and there is no way you
can use it without caring about it.

> Would not it be great if shared_ptr<>
> could figure out automatically that the
> pointed object has not been allocated
> dynamically so it should not call
> delete even wnen nobody references
> the object.
>
But this is not the expected behavior of shared_ptr<> and it is not a good
idea to change it in the way you suggest.

You can always forget about memory management or ownership issues, but you
can never ever forget about object lifetime issues because C++ is not
garbage-collected, which means that you are required to explicitly take care
of -or at least know about- object lifetimes.

In this case fool() is required to make some assumption about the lifetime
of 'p'. It can't ignore it.
In your situation, you should assume that 'p' will still be valid up to the
ending brace of fool(), when the function returns, and so do nothing.. that
is, don't use any smart pointer at all.

Whenever you use a smart pointer, in any way, you are explicitly stating
ownership.

HTH,

Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com


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