shared_ptr release [duplicate]

不问归期 提交于 2019-12-11 01:15:47

问题


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).


回答1:


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.



来源:https://stackoverflow.com/questions/7603876/shared-ptr-release

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!