Detach a pointer from a shared_ptr? [duplicate]

What you’re looking for is a release function; shared_ptr doesn’t have a release function. Per the Boost manual: 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() == … Read more

Where is shared_ptr?

There are at least three places where you may find shared_ptr: If your C++ implementation supports C++11 (or at least the C++11 shared_ptr), then std::shared_ptr will be defined in <memory>. If your C++ implementation supports the C++ TR1 library extensions, then std::tr1::shared_ptr will likely be in <memory> (Microsoft Visual C++) or <tr1/memory> (g++’s libstdc++). Boost … Read more

How is the std::tr1::shared_ptr implemented?

shared_ptr must manage a reference counter and the carrying of a deleter functor that is deduced by the type of the object given at initialization. The shared_ptr class typically hosts two members: a T* (that is returned by operator-> and dereferenced in operator*) and a aux* where aux is a inner abstract class that contains: … Read more

How to release pointer from boost::shared_ptr?

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 … Read more

std::shared_ptr initialization: make_shared() vs shared_ptr(new Foo) [duplicate]

Both examples are rather more verbose than necessary: std::shared_ptr<int> p(new int); // or ‘=shared_ptr<int>(new int)’ if you insist auto p = std::make_shared<int>(); // or ‘std::shared_ptr<int> p’ if you insist What’s the difference? The main difference is that the first requires two memory allocations: one for the managed object (new int), and one for the reference … Read more

Boost async_* functions and shared_ptr’s

In short, boost::bind creates a copy of the boost::shared_ptr<Connection> that is returned from shared_from_this(), and boost::asio may create a copy of the handler. The copy of the handler will remain alive until one of the following occurs: The handler has been called by a thread from which the service’s run(), run_one(), poll() or poll_one() member … Read more

C++ – passing references to std::shared_ptr or boost::shared_ptr

I found myself disagreeing with the highest-voted answer, so I went looking for expert opinons and here they are. From http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2011-Scott-Andrei-and-Herb-Ask-Us-Anything Herb Sutter: “when you pass shared_ptrs, copies are expensive” Scott Meyers: “There’s nothing special about shared_ptr when it comes to whether you pass it by value, or pass it by reference. Use exactly the … Read more