C++ 如何有意删除 boost::shared_ptr?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/621233/
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
How to intentionally delete a boost::shared_ptr?
提问by Frank
I have many boost::shared_ptr<MyClass>
objects, and at some point I intentionally want to delete
some of them to free some memory. (I know at that point that I will never need the pointed-to MyClass
objects anymore.) How can I do that?
我有很多boost::shared_ptr<MyClass>
对象,在某些时候我故意想让delete
其中的一些释放一些内存。(那时我知道我再也不需要指向MyClass
对象了。)我该怎么做?
I guess you can't just call delete()
with the raw pointer that I get with get()
.
我想你不能只delete()
用我得到的原始指针调用get()
。
I've seen a function get_deleter(shared_ptr<T> const & p)
in boost::shared_ptr
, but I'm not sure how to use it, and also it says experimentalright next to it. (I think I have Boost 1.38.)
我见过一个函数get_deleter(shared_ptr<T> const & p)
中boost::shared_ptr
,但我不知道如何使用它,并且还表示,实验就在旁边。(我想我有 Boost 1.38。)
Maybe just assign a new empty boost::shared_ptr
to the variable? That should throw away the old value and delete it.
也许只是boost::shared_ptr
为变量分配一个新的空?那应该扔掉旧值并删除它。
回答by Johannes Schaub - litb
You just do
你只要做
ptr.reset();
See the shared_ptr manual. It is equivalent to
请参阅shared_ptr 手册。它相当于
shared_ptr<T>().swap(ptr)
You call reset
on every smart pointer that should not reference the object anymore. The last such reset
(or any other action that causes the reference count drop to zero, actually) will cause the object to be free'ed using the deleter automatically.
您调用reset
每个不应再引用该对象的智能指针。最后一个这样的reset
(或任何其他导致引用计数降为零的操作,实际上)将导致使用删除器自动释放对象。
Maybe you are interested in the Smart Pointer Programming Techniques. It has an entry about delayed deallocation.
回答by John Morrison
If you want to be able to intentionally delete objects (I do all the time) then you have to use single ownership. You have been lured into using shared_ptr when it is not appropriate to your design.
如果您希望能够有意删除对象(我一直这样做),那么您必须使用单一所有权。当 shared_ptr 不适合您的设计时,您已被引诱使用它。
回答by j_random_hacker
The whole point of boost::shared_ptr<T>
is that the pointee object will be deleted exactlyat the moment when no shared_ptr<T>
s point at it -- that is, when the last shared_ptr<T>
pointing at this object goes out of scope or is reassigned to point to a different object. So, all you have to do to delete an object is make sure there are no shared_ptr<T>
s pointing at it. E.g. if you only have a single shared_ptr<T>
called p
pointing at an object, either let it fall out of scope, or call p.reset()
(equivalent to p = NULL
for a plain pointer), or assign it to point at something else.
整点boost::shared_ptr<T>
是,而指向对象将被删除恰好在那一刻没有shared_ptr<T>
在IT方面一点-那就是,当最后shared_ptr<T>
这个对象指向进入的范围之或重新分配给指向不同的对象。因此,删除对象所需要做的就是确保没有shared_ptr<T>
s 指向它。例如,如果您只有一个shared_ptr<T>
被调用p
的对象指向一个对象,要么让它超出范围,要么调用p.reset()
(相当于p = NULL
普通指针),或将其分配给其他对象。
If you have two shared_ptr<T>
s pointing at the object, you'll need to reassign both of them.
如果有两个shared_ptr<T>
s 指向对象,则需要重新分配它们。
EDIT:Thanks to dehmann for pointing out that p = NULL;
is not actually valid code for a shared_ptr<T>
... :)
编辑:感谢 dehmann 指出这p = NULL;
实际上不是一个有效的代码shared_ptr<T>
...... :)
回答by chrish
What you want to do is return weak references using boost::weak_ptrthat can be converted to a shared_ptr when needed. This can allow you to control the lifetime of the object in the shared_ptr and those that want to access it can hold onto the weak_ptr and try to convert to a shared_ptr. If that convert fails, then they can re-query and bring the object back into memory.
您想要做的是使用boost::weak_ptr返回弱引用,可以在需要时将其转换为 shared_ptr。这可以让你控制 shared_ptr 中对象的生命周期,那些想要访问它的人可以持有 weak_ptr 并尝试转换为 shared_ptr。如果转换失败,那么他们可以重新查询并将对象带回内存。