Boost logo

Boost Users :

From: Duane Murphy (duanemurphy_at_[hidden])
Date: 2003-07-27 11:17:48


--- At Sat, 26 Jul 2003 19:57:17 +0000, Joshua wrote:

>I've been reading through posts on here and in the docs but I think I may
>have missed something. Heres what I am trying to do:
>lets say I have a class Foo* and I'm trying to use it in a
>std::vector<shared_ptr<Foo> >. I can add them all day long and it works
>fine. The problem is that when I try to remove the shared_ptr from the
>vector. I expected it to delete the memory for the Foo* then delete
>itself (to completely free the memory that the shared_ptr used and the
>memory that the Foo was using. I think this is not happening though since
>the destructor is not being called when the elements are removed from the
>vector. Heres how I am currently inserting the shared_ptr's into the
>vector (yes I know this is the wrong way now that I've read a few of the
>posts here):
>
>typedef std::vector<boost::shared_ptr<Foo> > FooVector;
>
>FooVector Foobar;
>Foo* blah = new Foo();
>boost::shared_ptr<Foo> * NewPointer = new boost::shared_ptr<Foo>(blah);
>Foobar.insert(*NewPointer); //Yes I know this looks horrible. And its
>probably wrong.
>
>
>When I call Foobar.erase() I thought that the shared_ptr would be removed
>and the memory freed (and Foo's destructor called). I tried it like this :
>FooVector::iterator CurrentFoo = Foobar.begin();
>Foobar.erase(CurrentFoo);
>
>That did remove the shared_ptr but the Foo* did not seem to get
>deallocated, its destructor was never called. Some of the posts seem to
>indicate that this is a very bad way to let shared_ptr manager the
>memory, is there a better way to do what I want to do using shared_ptr or
>is shared_ptr the wrong tool to use?

There have been many good suggestions as to what the correct sequence for
inserting the shared_ptr into the vector. I'm not sure the real problem
has been adequately explained. Why isnt the foo* being deleted.

Shared_ptr<> counts the number of instances of the pointer that are
created. When you did:

boost::shared_ptr<Foo> * NewPointer = new boost::shared_ptr<Foo>(blah);

You created the first instance; count == 1.

When you did:

Foobar.insert(*NewPointer); //Yes I know this looks horrible. And its

A copy of the shared_ptr<> was made. The count is now 2.

When you did:

Foobar.erase(CurrentFoo);

One copy (of two) was "deleted". In this sense, the shared_ptr<> was
deleted. The destructor for the shared counter mearly decrements the
counter. Count == 1.

The problem is that the original count was leaked by using "new
shared_ptr<>". This is why all the suggestions show that you should be
using the stack based method for shared_ptr<>.

Shared_ptr is almost never allocated from the heap. There really is not
much point. It's use is as a value representation of pointer.

I hope this helps you understand where your code went astray.

Good luck,
 ...Duane

 ...Duane


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