Reputation: 1
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?
Upvotes: 0
Views: 496
Reputation: 30840
Assuming you fix the loop to call reset
on the actual shared_ptr
s instead of copies, the answer is "yes":
for (auto& it: interface_list) {
it.reset();
}
That leaves you with a list of dummy shared_ptr
instances, of course.
You can also just clear the vector and have C++ reset the pointers for you:
interface_list.clear();
or just let interface_list
go out of scope.
Upvotes: 2