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
37
votes
1 answer

Why is GoogleMock leaking my shared_ptr?

I use GoogleMock/GoogleTest for testing, and I'm seeing some strange behavior when a matcher has a shared_ptr to a mock as a parameter, and EXPECT is called on the same shared_ptr. The offending piece of code: #include #include…
bruno nery
  • 2,022
  • 2
  • 20
  • 31
36
votes
2 answers

How to properly duplicate an object given its shared_ptr

I'm trying to make a duplicate of an object of a custom class Event. I have a shared pointer to the object that I've obtained from its allocation: std::shared_ptr e = std::make_shared(); In order to get a true duplicate of e (not just…
Marc
  • 767
  • 1
  • 10
  • 23
35
votes
2 answers

How does shared_ptr know which destructor to use?

I wrote the following code to see how a shared_ptr would behave when it is the last reference to a shared_ptr and is itself destroyed. #include #include #include using namespace std; struct Thing{ …
Anne Quinn
  • 12,609
  • 8
  • 54
  • 101
34
votes
5 answers

How do smart pointers choose between delete and delete[]?

Consider: delete new std :: string [2]; delete [] new std :: string; Everyone knows the first is an error. If the second wasn't an error, we wouldn't need two distinct operators. Now consider: std :: unique_ptr x (new int [2]); std ::…
spraff
  • 32,570
  • 22
  • 121
  • 229
34
votes
2 answers

std::shared_ptr Exception Safety

I just realised reading this page that the constructor of std::shared_ptr with a single pointer argument is not noexcept. Hence the following code contains a possible memory leak: std::shared_ptr p3 (new int); The reasonning is that two…
Arnaud
  • 3,765
  • 3
  • 39
  • 69
34
votes
3 answers

Using shared_from_this in templated classes

I have a resource manager that, like Andrei Alexandrescu proposed in the book Modern C++ Design, follows a policy based design. I am having trouble though, because my resource manager needs to be able to provide references to itself to the managed…
nikolas
  • 8,707
  • 9
  • 50
  • 70
34
votes
1 answer

Use of enable_shared_from_this with multiple inheritance

BI am using enable_shared_from_this in my code, and I am not sure if its usage is correct. This is the code: class A: public std::enable_shared_from_this { public: void foo1() { auto ptr = shared_from_this(); } }; class…
33
votes
6 answers

Passing const shared_ptr& versus just shared_ptr as parameter

I've been reading quite a number of discussions about performance issues when smart pointers are involved in an application. One of the frequent recommendations is to pass a smart pointer as const& instead of a copy, like this: void…
Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
33
votes
1 answer

Why is std::weak_ptr::expired optimized away?

In the following code, while ( !Ref.expired() ); is joyfully optimized into an infinite loop. If the line of code is changed to while ( !Ref.lock() );. everything works as expected. So two questions really: 1) How can the compiler optimize away…
Puff Of Hot Air
  • 331
  • 2
  • 4
32
votes
7 answers

How does a reference-counting smart pointer's reference counting work?

In other words, how does the implementation keeps track of the count? Is there a map-like object maintained which is accessible by all the shared_ptr instances whose key is the pointer's address and value is the number of references? If I've to…
Srikanth
  • 11,780
  • 23
  • 72
  • 92
32
votes
5 answers

Is boost shared_ptr thread safe?

I have a question about boost::shared_ptr. There are lots of thread. using namespace boost; class CResource { // xxxxxx } class CResourceBase { public: void SetResource(shared_ptr res) { m_Res = res; } …
user25749
  • 4,825
  • 14
  • 61
  • 83
32
votes
1 answer

What is going on: C++ std::move on std::shared_ptr increases use_count?

I was always assuming that std::move() on a std::shared_ptr steals the pointer and sets the pointer of the original to nullptr-thus not increasing the reference count. That does not seem to be true in my world. SETUP: MacOS, g++ -version => "Apple…
Frank-Rene Schäfer
  • 3,182
  • 27
  • 51
32
votes
2 answers

Trivial cases of shared_ptr and weak_ptr failing

I'm having trouble using shared_ptr and weak_ptr along with enable_shared_from_this. When I google the symptoms of what I'm seeing, everybody suggests "you cannot use shared_from_this() when there are no shared_ptr instances owning your object. But…
32
votes
1 answer

Set shared_ptr with new_pointer that is old_pointer + offset

Here is a smart pointer: std::shared_ptr p(new char[size]) which represents array filled with raw binary file content. After (and only after) the whole array is copied from file to RAM, I can parse it, and during this I retrieve some header…
Alexey S. Larionov
  • 6,555
  • 1
  • 18
  • 37
32
votes
5 answers

What is diffrence between lock() and expired()? weak_ptr C++

Recently I started at C++11. I studied about weak_ptr. There exist two ways of getting raw pointer. lock() function shared_ptr spFoo = wpPtr.lock(); if(spFoo) { spFoo->DoSomething(); } expired() function if(!wpPtr.expired()) { …
zeno
  • 351
  • 1
  • 3
  • 7