mardi 8 novembre 2022

Is shared_ptr always free memory automatically?

You are partially replacing old code from the past. I'm replacing an existing pointer with a smart pointer for this operation, and I'm curious about deallocation. I replaced the code as below, will a leak occur?

std::vector<std::shared_ptr<Interface>> interface_list;

for (int i = 0 ; i < 5; ++i) {
  Interface *buffer = CreateFactory(i);
  if (buffer) interface_list.emplace_back(buffer);
}
...
for (std::shared_ptr<Interface> it: interface_list) {
  it.reset();
}
//IS iT ok?

Aucun commentaire:

Enregistrer un commentaire