3

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

Community
  • 1
  • 1
David
  • 27,652
  • 18
  • 89
  • 138

1 Answers1

5

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.

Community
  • 1
  • 1
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
  • I actually just solved this after reading this.... http://stackoverflow.com/questions/1833356/detach-a-pointer-from-a-shared-ptr/5995770#5995770 – David Sep 29 '11 at 22:23