Sign in to follow this  
gumpy

std::vector/boost::shared_ptr question

Recommended Posts

this is my first time using anything from the boost libraries. the code is short and simple. i create a vector of shared_ptr<myclass>, copy the vector, and erase an element of the copied vector.
#include <boost/shared_ptr.hpp>
#include <vector>
#include <iostream>

class myclass
{
        int m_i;
public:
        myclass(int i):m_i(i){std::cout << "myclass created: "<< m_i << std::endl;}
        ~myclass(){std::cout << "myclass destroyed: " << m_i << std::endl;}
};

int main(int argc, char *argv[])
{
        std::vector<boost::shared_ptr<myclass> > v;
        for(int i=0;i<5;++i)
                v.push_back(boost::shared_ptr<myclass>(new myclass(i)));
        std::vector<boost::shared_ptr<myclass> > v2(v);
        v2.erase(v2.begin()+2); // erase 3rd element
        return 0;
}

the following output shows that the object isn't deleted until the end of main().
myclass created: 0
myclass created: 1
myclass created: 2
myclass created: 3
myclass created: 4
myclass destroyed: 0
myclass destroyed: 1
myclass destroyed: 2
myclass destroyed: 3
myclass destroyed: 4
this next source i instead erase the element from the original vector.
#include <boost/shared_ptr.hpp>
#include <vector>
#include <iostream>

class myclass
{
        int m_i;
public:
        myclass(int i):m_i(i){std::cout << "myclass created: "<< m_i << std::endl;}
        ~myclass(){std::cout << "myclass destroyed: " << m_i << std::endl;}
};

int main(int argc, char *argv[])
{
        std::vector<boost::shared_ptr<myclass> > v;
        for(int i=0;i<5;++i)
                v.push_back(boost::shared_ptr<myclass>(new myclass(i)));
        std::vector<boost::shared_ptr<myclass> > v2(v);
        v.erase(v.begin()+2);   // erase 3rd element
        return 0;
}

the output shows that the class instance is deleted when the element is erased from the original vector.
myclass created: 0
myclass created: 1
myclass created: 2
myclass created: 3
myclass created: 4
myclass destroyed: 2
myclass destroyed: 0
myclass destroyed: 1
myclass destroyed: 3
myclass destroyed: 4
now the question is, am i misunderstanding the shared_ptr or the stl container?

Share this post


Link to post
Share on other sites
Whether or not you misunderstood what boost::shared_ptr or std::vector does would depend on what you expect them to do. Since you never said that, we can't answer that.

However, to give a possible explanation on the output.

In the first code, you have the pointers to numbers 0-4 (in the form of myclass's of course), v1, and v2 contains the same set of pointers. Then you erase the third element from v2, and only v1 have a copy of this pointer now. When main is ended, v2 is destroyed, but since all pointers in v2 also exists in v1, none of them are released. All pointers, 0-4, will be released when v1 is destroyed.

In the second code, the same scenario as above, only that v2 is the only set with the erased element. v2 is destroyed first, but only the 2-element is unique to v2, so that's the only element being released, since all other elements exists in v1 aswell. The rest is released when v1 is dedestroyed.

boost::shared_ptr is a reference counted pointer, meaning boost::shared_ptr keeps track of how many copies there are of the pointer. The pointed to object is not released until the last boost::shared_ptr to that particular object is being destroyed. In your case, you have two vectors, each having a copy of a few pointers, meaning there are two copies of each object.

Share this post


Link to post
Share on other sites
But both vectors go out of scope at the end of main in both examples.

EDIT:
I see now, its to do with the destruction order of v and v2. Still the important thing is that all the elements are properly freed.

Share this post


Link to post
Share on other sites
i should have been more thorough with messages and deconstructing the vectors myself and i wouldn't made such a silly mistake. i somehow convinced myself the class instance was deleted the instant i was erasing the element. thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this