shared_ptr release
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.
David
Updated on June 04, 2022Comments
-
David almost 2 years
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).
-
Drew Delano over 12 yearsWhy do you want to subvert
shared_ptr
? -
sehe over 12 yearsThe deleter accompanies the NEW value
-
Mooing Duck over 12 yearsThe only way to release it is probably to give someone else a shared_ptr, or rethink your entire design
-
-
David over 12 yearsI actually just solved this after reading this.... stackoverflow.com/questions/1833356/…