Search code examples
c++vectorshared-ptr

Calling clear() on a vector of shared_ptr. Will memory be freed?


I have a std::vector member of boost::shared_ptr to objects of class Foo inside a class. A function SetData() adds a pointer to a new object of Foo to the vector. The contructor of Foo makes an internal copy of the data pointed to by pData. Now when I call my Reset() function, will all the memory actually be freed?

class myClass()
{
 void SetData(char* pData, size_t nSize)
 {
   boost::shared_ptr<Foo> pFoo(new Foo(pData, nSize));
   mVector.push_back(pFoo);
 }

 void Reset()
 {
   mVector.clear();
 }

private:
    std::vector<boost::shared_ptr<Foo>> mVector;


};

Solution

  • Yes.​​​​​​​​​​​​​​​​​​​​​​​​​​​