C++ 是否在 std::shared_ptr 上使用 .reset() 删除所有实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21589595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:43:25  来源:igfitidea点击:

Does using .reset() on a std::shared_ptr delete all instances

c++memorymemory-leaksshared-memoryshared-ptr

提问by zeus_masta_funk

I'm new to shared_ptr's and I'm trying to figure out the exact functionality of the .reset() function.

我是 shared_ptr 的新手,我试图弄清楚 .reset() 函数的确切功能。

#include <memory>
#include <stdio>

using namespace std;
class SomeClass{};

int main() 
{
   shared_ptr<SomeClass> sp (nullptr);

   //do some stuff, sp now has 10 co-owners

   cout << sp.use_count << endl;
   sp.reset();
   cout << sp.use_count << endl;
   return 0;
}

Would output

会输出

10
0

So since I used the reset function are all instances deleted from memory? As in, have I just eliminated any possible memory leaks with sp? Obviously this was a toy example that I quickly made up, sorry if it has any errors.

那么自从我使用了重置功能后,所有实例都从内存中删除了吗?就像在,我刚刚用 sp 消除了任何可能的内存泄漏吗?显然,这是我很快编造的一个玩具示例,如果有任何错误,请见谅。

Follow up situation:

跟进情况:

shared_ptr<SomeClass> returnThis() {
    shared_ptr<SomeClass> someObject(new SomeClass(/*default constructor for example*/) );
    return someObject;
}

somehere in main:

某处主要:

shared_ptr<SomeClass> mainObject;
mainObject = returnThis();

Does mainObject have a use count of 2 because someObject was created in a function but never cleared? Or is it one and the clean-up is done automatically when returning the value?

mainObject 的使用计数是否为 2,因为 someObject 在函数中创建但从未清除?或者它是一个并且在返回值时自动完成清理?

回答by Dietrich Epp

When you use .reset(), you are eliminating oneowner of the pointer, but all of the other owners are still around. Here is an example:

当您使用 时.reset(),您正在消除指针的一个所有者,但所有其他所有者仍然存在。下面是一个例子:

#include <memory>
#include <cstdio>

class Test { public: ~Test() { std::puts("Test destroyed."); } };

int main()
{
    std::shared_ptr<Test> p = std::make_shared<Test>();
    std::shared_ptr<Test> q = p;
    std::puts("p.reset()...");
    p.reset();
    std::puts("q.reset()...");
    q.reset();
    std::puts("done");
    return 0;
}

The program output:

程序输出:

p.reset()...
q.reset()...
Test destroyed.
done

Note that pand qare both owners of the object, and once both pand qare reset, thenthe instance is destroyed.

请注意,pandq都是对象的所有者,一旦pandq都被重置,实例将被销毁。

回答by Alex Telishev

No.

不。

The whole purpose of shared_ptris that you cannot delete it from one place if someone is using it in another. shared_ptr::reset()just decreases use_countby one and replaces its object by nullptr.

的全部目的shared_ptr是如果有人在另一个地方使用它,你不能从一个地方删除它。shared_ptr::reset()只是减use_count一并用 替换它的对象nullptr

回答by Yochai Timmer

The .reset() method only applies to the object it's called upon.

.reset() 方法仅适用于它被调用的对象。

It just replaces the pointer that variable is holding.

它只是替换变量持有的指针。