tags:

views:

797

answers:

5

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

+6  A: 

Why would you want to do this? It smells like you're misusing or misunderstanding how to use smart pointers.

Dustin Getz
You'd want to do this when you have a `void sink(auto_ptr<T> p);`
MSalters
+2  A: 

You can delete the shared pointer, which seems much the same to me. If pointers are always unique, then std::auto_ptr<> is a good choice. Bear in mind that unique pointers can't be used in STL containers, since operations on them do a lot of copying and temporary duplication.

David Thornley
+4  A: 

To let the pointer point to nothing again, you can call shared_ptr::reset().

However, this will delete the object pointed to when your pointer is the last reference to the object. This, however, is exactly the desired behaviour of the smart pointer in the first place.

If you just want a reference that does not hold the object alive, you can create a boost::weak_ptr (see boost documentation). A weak_ptr holds a reference to the object but does not add to the reference count, so the object gets deleted when only weak references exist.

Timbo
+4  A: 

You could use fake deleter. Then pointers will not be deleted actually.

struct NullDeleter {template<typename T> void operator()(T*) {} };

// pp of type some_t defined somewhere
boost::shared_ptr<some_t> x(pp, NullDeleter() );
Kirill V. Lyadvinsky
lol, interesting snippet
Dustin Getz
It is interesting, but what would be the purpose of using a shared_ptr in the first place?
UncleBens
@UncleBens, such deleter has very limited usage. For instance, when you need to call a function and pass to it something like `shared_ptr<some_t>( this )`. That will be safe only with `NullDeleter`.
Kirill V. Lyadvinsky
Yup, we used something like this when the interface (which we didn't control) required a shared_ptr and guaranteed that it wouldn't store the pointer past the function call.
McBeth
+6  A: 

Don't. Boost's FAQ entry:

Q. Why doesn't shared_ptr provide a release() function?

A. shared_ptr cannot give away ownership unless it's unique() because the other copy will still destroy the object.

Consider:

shared_ptr<int> a(new int);
shared_ptr<int> b(a); // a.use_count() == b.use_count() == 2

int * p = a.release();

// Who owns p now? b will still call delete on it in its destructor.

Furthermore, the pointer returned by release() would be difficult to deallocate reliably, as the source shared_ptr could have been created with a custom deleter.

So, this would be safe in case it's the only shared_ptr instance pointing to your object (when unique() returns true) and the object doesn't require a special deleter. I'd still question your design, if you used such a .release() function.

sellibitze