共享指针释放
我正在尝试释放一个shared_ptr(释放 unique_ptr 的方式)。我知道当shared_ptr不唯一时这没有意义,但我有一个保证是唯一的shared_ptr。我已经尝试过......
m_pObj.reset((T*)nullptr, [](T* const){});
但它只是删除了该对象。我不确定如果shared_ptr 最终调用delete,删除器参数有什么好处。
有什么方法可以实现这一点(如果没有其他方法,欢迎使用VS2010特定的解决方案)。
Possible Duplicate:
How to release pointer from boost::shared_ptr?
Detach a pointer from a shared_ptr?
I'm attempting to release a shared_ptr (the way you can release a unique_ptr). I know this doesn't make sense when the shared_ptr isn't unique, but I have a shared_ptr which is guaranteed to be unique. I've tried...
m_pObj.reset((T*)nullptr, [](T* const){});
...but it just deletes the object anyway. I'm not sure what that deleter argument is good for if shared_ptr winds up calling delete anyway.
Is there any way to achieve this (solutions specific to VS2010 are welcome if there is no other way).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这个问题作为 dup 关闭之前,我会添加我对此的看法。
我认为有很多很好的理由让您偶尔想要释放一个shared_ptr(或boost::scoped_ptr)。然而,设计这些类的人认为你不应该这样做。 (这是他们的孩子,他们有权利这样做。)
据我所知,根本不可能分离shared_ptr。您将需要使用另一个类或提出一个不需要分离它的设计。
我应该阅读这些骗局,就像你实际上可以使用非常特殊的删除器将其破解在一起。
Before this get's closed as dup, I'll add my take on this.
I think there are a lot of good reasons why you would occasionally want to release a shared_ptr (or boost::scoped_ptr). However, the people who designed these classes think that you should not be able to do this. (It's their baby, they have a right to.)
As far as I can see, it is simply not possible to detach a shared_ptr. You will need to either use another class or come up with a design where you do not need to detach it.
And I should read the dupes as you can actually hack it together using a very special deleter.