Alex Rivera | Logout

How to release pointer from boost::shared_ptr?

Asked 2009-10-06T13:57:02.243
41

Can boost::shared_ptr release the stored pointer without deleting it?

I can see no release function exists in the documentation, also in the FAQ is explained why it does not provide release function, something like that the release can not be done on pointers that are not unique. My pointers are unique. How can I release my pointers ? Or which boost smart pointer class to use that will allow me releasing of the pointer ? I hope that you won't say use auto_ptr :)

Edit
Report

1 Answer

0

Here's a hack that might work. I wouldn't recommend it unless you're in a real bind.

template<typename T>
T * release_shared(std::shared_ptr<T> & shared)
{
    static std::vector<std::shared_ptr<T> > graveyard;
    graveyard.push_back(shared);
    shared.reset();
    return graveyard.back().get();
}
answered 2011-09-29T22:31:01.697

Your Answer