擦除持有共享指针的容器也会删除指向的对象吗?
Erasing container that holds a shared pointer also deletes the object that's being pointed to?
例如,使用原始指针:
Object *obPointer = new Object(); //dynamically allocating memory, meaning we have to delete it, ourselves, later
std::unordered_map<std::string, Objects*> objContainer; //container that holds a string key and a pointer to an object type.
objContainer.emplace("A", obPointer); // placing a string and a pointer to an object into the container. simple enough.
现在,如果我们擦除那个容器,它不会释放我们分配的内存,“对象”类型。所以我们必须手动删除它,对吗?
delete obPointer;
objContainer.erase("A");
如果我们没有删除 obPointer,仅擦除容器是不够的 - 我们会发生内存泄漏。
无论如何,当谈到共享指针时,我不知道这是否有效,因为我们不对它们调用 delete:
std::shared_ptr<Object> obPointer = std::make_shared<Object>(); //shared pointer
std::unordered_map<std::string, std::shared_ptr<Object>> container;
container.emplace("A", obPointer);
container.erase("A");
智能指针自己清理了吗?或者它只会在超出范围时自行清理?
无论最后发生什么
因为你有一个共享指针,它会跟踪它存在多少个副本。在您的情况下,您的本地范围内有 obPointer
并且 container
内有一个对象(因此总共有 2 个)。
在调用 erase
后,container
中的对象被破坏并且计数返回到 1。当 obPointer
超出范围时,计数变为 0 并且 QObject
实例被删除。
所以,就像 Dave 提到的那样,我应该使用一个独特的指针。在我的示例中,通过擦除容器,它不会释放使用 shared_ptr 分配的内存。如果 运行 超出范围或引用,它只会删除内存。
因此,正如 Dave 所建议的,我使用 unique_pointer 来创建内存,然后我使用“std::move”将它“传递”给容器,t运行 将所有权转移给容器的指针。因此,一旦我擦除该容器,内存就会释放。像这样:
//Container
std::unordered_map<std::string, std::unique_ptr<Object>> //container;
{//Scope just to demonstrate it works
std::unique_ptr<Object> obPointer = std::make_unique<Object>(); //unique pointer
/*Now we transfer the ownership to the container, making its life spam
rely on the scope of the CONTAINER and the container itself*/
container.emplace("A", std::move(obPointer)); //std::move very important
}//only "obPointer" will die cuz of the scope, but it's null now.
// the actual object that we created didn't delete itself. It's in the container
/*If we want to free up the memory, we should just erase the specific container
or wait for it to be out of the scope.*/
container.erase("A"); //pointer deleted and memory freed
给你。一种“手动”删除智能指针的方法。很有用。
例如,使用原始指针:
Object *obPointer = new Object(); //dynamically allocating memory, meaning we have to delete it, ourselves, later
std::unordered_map<std::string, Objects*> objContainer; //container that holds a string key and a pointer to an object type.
objContainer.emplace("A", obPointer); // placing a string and a pointer to an object into the container. simple enough.
现在,如果我们擦除那个容器,它不会释放我们分配的内存,“对象”类型。所以我们必须手动删除它,对吗?
delete obPointer;
objContainer.erase("A");
如果我们没有删除 obPointer,仅擦除容器是不够的 - 我们会发生内存泄漏。 无论如何,当谈到共享指针时,我不知道这是否有效,因为我们不对它们调用 delete:
std::shared_ptr<Object> obPointer = std::make_shared<Object>(); //shared pointer
std::unordered_map<std::string, std::shared_ptr<Object>> container;
container.emplace("A", obPointer);
container.erase("A");
智能指针自己清理了吗?或者它只会在超出范围时自行清理?
无论最后发生什么
因为你有一个共享指针,它会跟踪它存在多少个副本。在您的情况下,您的本地范围内有 obPointer
并且 container
内有一个对象(因此总共有 2 个)。
在调用 erase
后,container
中的对象被破坏并且计数返回到 1。当 obPointer
超出范围时,计数变为 0 并且 QObject
实例被删除。
所以,就像 Dave 提到的那样,我应该使用一个独特的指针。在我的示例中,通过擦除容器,它不会释放使用 shared_ptr 分配的内存。如果 运行 超出范围或引用,它只会删除内存。 因此,正如 Dave 所建议的,我使用 unique_pointer 来创建内存,然后我使用“std::move”将它“传递”给容器,t运行 将所有权转移给容器的指针。因此,一旦我擦除该容器,内存就会释放。像这样:
//Container
std::unordered_map<std::string, std::unique_ptr<Object>> //container;
{//Scope just to demonstrate it works
std::unique_ptr<Object> obPointer = std::make_unique<Object>(); //unique pointer
/*Now we transfer the ownership to the container, making its life spam
rely on the scope of the CONTAINER and the container itself*/
container.emplace("A", std::move(obPointer)); //std::move very important
}//only "obPointer" will die cuz of the scope, but it's null now.
// the actual object that we created didn't delete itself. It's in the container
/*If we want to free up the memory, we should just erase the specific container
or wait for it to be out of the scope.*/
container.erase("A"); //pointer deleted and memory freed
给你。一种“手动”删除智能指针的方法。很有用。