jeudi 3 octobre 2019

Why does the shared pointer does not delete memory?

shared_ptr<string> pNico(new string("Nico"));
shared_ptr<string> pJutta(new string("Jutta"));
// put them multiple times in a container
vector<shared_ptr<string>> whoMadeCoffee;
whoMadeCoffee.push_back(pJutta);
whoMadeCoffee.push_back(pJutta);
whoMadeCoffee.push_back(pNico);
whoMadeCoffee.push_back(pJutta);
whoMadeCoffee.push_back(pNico);

pNico = nullptr;         
whoMadeCoffee.resize(2);

Such a deletion does not necessarily have to happen at the end of the scope.

(from Josuttis, Nicolai M.. The C++ Standard Library.)

My question is why in above mention case it is not guaranteed that memory of "Nico" object will be deleted at the end of the scope?

Aucun commentaire:

Enregistrer un commentaire