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;
};
will all the memory actually be freed?
It rather depends on what you're asking. On the surface of it, yes.
The entire purpose of smart pointers is that they manage memory for you, and the entire purpose of shared pointers it that the thing they point to is automatically freed when no more shared pointers point to it.
When you clear the vector, the shared pointers it contains are destroyed, and this action automatically de-allocates any encapsulated objects with no more shared pointers referring to them.
Now, does that free all memory? Not necessarily. We have no idea what Foo
does; if you didn't properly implement RAII in it, then it's possible that Foo
leaks memory and the answer becomes no.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With