Boost logo

Boost Users :

From: John Harris (john.harris_at_[hidden])
Date: 2003-05-01 09:35:53


--- In Boost-Users_at_[hidden], Darin Adler <darin_at_b...> wrote:
> On Thursday, May 1, 2003, at 04:57 AM, Hossein Haeri wrote:
>
> > So the true answer is not to forget to call the reset() member
> > function for every shared_ptr<>, before the end of it's scope.
>
> No, that won't do any good. When you do a reset(), the shared_ptr
> relinquishes ownership of the thing it points to. If it's the sole
> owner, it will destroy the object. It's simply an error to put a
> pointer to a stack object inside a shared_ptr; you can't correct
that
> error with a reset() call.
>
> -- Darin

I agree with Darin. Shared_ptr is used for automatically deleting a
heap object when the last reference to it (that is, the last copy of
the shared_ptr managing it) goes out of scope. It keeps count as the
shared_ptr is copied around through parameters and return values and
is constructed and destroyed. The underlying count goes up on
construction and down on destruction. When the count gets to
zero, 'delete' is called on your object.

It is used to prevent copies of an object from being made.

Hossein, if you'll post a description of what you really want to do,
then we might be able to suggest a better idiom for you to follow.

jh


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net