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
12
votes
7 answers

C++: auto_ptr + forward declaration?

I have a class like this: class Inner; class Cont { public: Cont(); virtual ~Cont(); private: Inner* m_inner; }; in the .cpp, the constructor creates an instance of Inner with new and the destructor deletes it. This is working pretty…
shoosh
  • 76,898
  • 55
  • 205
  • 325
12
votes
3 answers

atomic operations for shared_ptr in C++11

By reading the c++11 draft n3242, section 20.7.2.5, looks like we have atomic operations on shared_ptr, which enables us do lock-free on complicated structure without worrying about GC/memory leak. However, I couldn't use it successfully in…
Kan Li
  • 8,557
  • 8
  • 53
  • 93
12
votes
4 answers

Multiple shared_ptr storing same pointer

Consider this program: #include #include class X : public std::enable_shared_from_this { public: struct Cleanup1 { void operator()(X*) const; }; struct Cleanup2 { void operator()(X*) const; }; std::shared_ptr
aschepler
  • 70,891
  • 9
  • 107
  • 161
12
votes
2 answers

Initializing shared_ptr member variable, new vs make_shared?

When initializing a shared_ptr member variable: // .h class Customer { public: Customer(); private: std::shared_ptr something_; } // .cpp Customer(): something_(new OtherClass()) { } vs. Customer(): …
User
  • 62,498
  • 72
  • 186
  • 247
11
votes
3 answers

A non-shared smart pointer with incomplete types

Is there a standard pointer class (or Boost) which is a non-shared pointer that works with incomplete types? I've gone over the C++11 standard and the boost library and can't find one, though it seems like a very useful type. For example, I'd like…
edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267
11
votes
2 answers

why is std::shared_ptr using atomic cpu operations

I have a problem understanding why shared_ptr is using atomic cpu instructions... I cant figure out the reasons because it is NOT thread safe. Can somebody please explain. If you wonder know how I know that it uses atomic intstuructions: there was…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
11
votes
4 answers

Locking a shared_ptr

I have an shared object that need to be send to a system API and extract it back later. The system API receives void * only. I cannot use shared_ptr::get() because it do not increases the reference count and it could be released by other threads…
Earth Engine
  • 10,048
  • 5
  • 48
  • 78
11
votes
4 answers

Why does shared_ptr needs to hold reference counting for weak_ptr?

Quoted from C++ Primer $12.1.6: A weak_ptr (Table 12.5) is a smart pointer that does not control the lifetime of the object to which it points. Instead, a weak_ptr points to an object that is managed by a shared_ptr. Binding a weak_ptr to a…
choxsword
  • 3,187
  • 18
  • 44
11
votes
1 answer

Memory order in shared pointer destructor

I'm trying to figure out the most relaxed (and correct) memory order for shared pointer destructor. What I have in mind for now is as follows: ~shared_ptr() { if (p) { if (p->cnt.fetch_sub(1, std::memory_order_release) == 1) { …
Lingxi
  • 14,579
  • 2
  • 37
  • 93
11
votes
1 answer

Why do const shared_ptr& and const shared_ptr& show different reference counts?

For the following code snippet, it shows different reference counts in the methods. Could someone explain why these values are different? class Foo { }; void f1( const std::shared_ptr& ptr ) { std::cout << "f1(): counts: " <<…
11
votes
1 answer

Why is creating shared_ptr from unique_ptr of array not allowed anymore?

From cppreference: In C++11 and C++14 it is valid to construct a std::shared_ptr from a std::unique_ptr: std::unique_ptr arr(new int[1]); std::shared_ptr ptr(std::move(arr)); Since the shared_ptr obtains its deleter (a …
ikh
  • 10,119
  • 1
  • 31
  • 70
11
votes
1 answer

shared_ptr strangeness with null values and custom deleter

We recently came across a crash when moving from a unique_ptr to a shared_ptr, using a custom deleter. The crash happened when the pointer used to create the smart pointer was null. Below is code that reproduces the problem, and shows two cases that…
Simon Parker
  • 1,656
  • 15
  • 37
11
votes
1 answer

Boost intrusive pointer

I'm a little confused about boost's intrusive pointer. The definition says: "Every new intrusive_ptr instance increments the reference count by using an unqualified call to the function intrusive_ptr_add_ref, passing it the pointer as an…
Benjamin Larsen
  • 560
  • 1
  • 7
  • 21
11
votes
8 answers

smart pointers + "this" considered harmful?

In a C++ project that uses smart pointers, such as boost::shared_ptr, what is a good design philosophy regarding use of "this"? Consider that: It's dangerous to store the raw pointer contained in any smart pointer for later use. You've given up…
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
11
votes
2 answers

static member function make_shared of shared_ptr

Using libc++ I find out std::shared_ptr::make_shared() static member function in public section. It is very handy when I have already defined type alias to std::shared_ptr's specialization: using T = int; using P = std::shared_ptr< T >; auto p =…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169