I realized that after calling vector.clear()
which hold shared pointers, the destructors of the object which own by shared_ptr
is not being released.
Code example can be seen below . Even vector.clear()
being called, destructor called after shared pointer goes beyond the scope.My question is - do I have to delete all smart pointers inside the vector manually by resetting them? Is there any easier way that you can advice ?
Output :
constructor
I am here
destructor
Code:
#include <vector>
#include <iostream>
#include <memory>
using namespace std;
class A
{
public:
A(){cout << "constructor" << endl;};
~A(){cout << "destructor" << endl;};
};
int main( )
{
shared_ptr<A> sharedptr (new A);
std::vector<shared_ptr<A> > test;
test.push_back(sharedptr);
test.clear();
cout << "I am here" << endl;
}