Questions tagged [shared-ptr]

Reference counted smart pointer class implementing shared ownership

A shared_ptr is a non-intrusive smart pointer that manages the ownership of a shared resource. Multiple shared_ptr objects can share ownership of the same resource, which will be destroyed automatically when there are no more "non-empty" shared_ptr objects referring to it.

The different versions and implementation commonly used are boost::shared_ptr (the original), std::shared_ptr (included in the C++11 standard) and std::tr1::shared_ptr (from the TR1 report on library extensions, lacks certain features such as aliasing).

The related class template weak_ptr is designed for use with shared_ptr and represents a non-owning reference to a resource that is managed by a shared_ptr. This useful to avoid circular references. A weak_ptr can also be used as a "tracking reference" and converted to a shared_ptr to temporarily assume shared ownership when it is required.

3323 questions
20
votes
5 answers

Boost async_* functions and shared_ptr's

I frequently see this pattern in code, binding shared_from_this as the first parameter to a member function and dispatching the result using an async_* function. Here's an example from another question: void Connection::Receive() { …
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
20
votes
1 answer

C++11 atomics and intrusive shared pointer reference count

I am writing intrusive shared pointer and I am using C++11 facilities for reference counter. Here are the relevant fragments of my code: //... mutable std::atomic count; //... void SharedObject::addReference() const { …
wilx
  • 17,697
  • 6
  • 59
  • 114
19
votes
1 answer

Deletion of pointer to incomplete type and smart pointers

When trying to use an auto_ptr with a type that was declared with forward-declaration, like this: class A; ... std::auto_ptr a; the destructor of A is not called (apparently, because auto_ptr internally deletes the underlying pointer and the…
user500944
19
votes
6 answers

How to handle 'this' pointer in constructor?

I have objects which create other child objects within their constructors, passing 'this' so the child can save a pointer back to its parent. I use boost::shared_ptr extensively in my programming as a safer alternative to std::auto_ptr or raw…
Kyle
  • 4,487
  • 3
  • 29
  • 45
19
votes
1 answer

std::shared_ptr deep copy object

Can't find much on that for C++11 but only on boost. Consider the following class: class State { std::shared_ptr _graph; public: State( const State & state ) { // This is assignment, and thus points to same object …
Ælex
  • 14,432
  • 20
  • 88
  • 129
19
votes
3 answers

Does the WKWYL optimization in make_shared<>() introduce a penalty for some multithreading applications?

A few days ago I happened to watch this very interesting presentation by Stephan T. Lavavej, which mentions the "We Know Where You Live" optimization (sorry for using the acronym in the question title, SO warned me the question might have been…
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
18
votes
3 answers

C++ std::shared_ptr usage and information

I am trying to use std::shared_ptr in my code. I have seen there have been other questions on the subject, but I am still getting a compiler error. Have I got the right version of gcc and setup? What I have done: I have tried to compile my code with…
MWright
  • 1,681
  • 3
  • 19
  • 31
18
votes
3 answers

Can deriving a class from 'enable_shared_from_this' increase performance?

make_shared is more performant than separately calling new and creating a shared_ptr because make_shared allocates space for the reference count and weak count in the same memory block as the client object instance (effectively giving the shared_ptr…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
18
votes
2 answers

Why the compiler complains that std::thread arguments must be invocable after conversion to rvalues?

Why the compiler complains if the the thread function delaration is changed to void thr(std::shared_ptr& p).Complie error: gcc-10.1.0/include/c++/10.1.0/thread: In instantiation of 'std::thread::thread(_Callable&&, _Args&& ...) [with…
sunshilong369
  • 646
  • 1
  • 5
  • 17
18
votes
3 answers

Why shared_ptr's reference counting object needs to keep track of the number of weak_ptrs pointing to the object too?

Hi I am reading through this document and some other documents about C++'s shared_ptr and they all seem to suggest that apart from the number of shared_ptr pointing to the allocated object, the reference count object has to keep track of how many…
Bob Fang
  • 6,963
  • 10
  • 39
  • 72
18
votes
3 answers

Extract a subvector from a vector without copy

I have a large size of vector and I want extract its subvector based on index. But I do not want to make a new copy of a subvector. Can I use pointer a iterator to return the pointer of the original vector? something like: vector orig = {…
duqiyaner
  • 181
  • 1
  • 1
  • 4
18
votes
4 answers

const shared_ptr to shared_ptr

How can one convert a shared_ptr that points to a const object to a shared_ptr that points to a non-const object. I am trying to do the following : boost::shared_ptr Ckk(new A(4)); boost::shared_ptr kk=const_cast< boost::shared_ptr >…
user152508
  • 3,053
  • 8
  • 38
  • 58
18
votes
3 answers

Using C++11 lambdas asynchronously, safely

I've come to C++11 from an Objective-C background, and one thing I'm struggling to come to terms with is the different capturing semantics of C++11 lambdas vs Objective-C "blocks". (See here for a comparison). In Objective-C, like C++, the self/this…
Nick Hutchinson
  • 5,044
  • 5
  • 33
  • 34
18
votes
3 answers

When would you use an std::auto_ptr instead of boost::shared_ptr?

We've pretty much moved over to using boost::shared_ptr in all of our code, however we still have some isolated cases where we use std::auto_ptr, including singleton classes: template < typename TYPE > class SharedSingleton { public: static…
Alan
  • 13,510
  • 9
  • 44
  • 50