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

Apple and shared_ptr

I seem to be missing something here. I moved from boost::shared_ptr to std::shared_ptr. shared_ptr was part of TR1 back in the mid-2000s, and it should be available everywhere in 2012. Trying to use shared_ptr under Apple gets me a slew of undefined…
jww
  • 97,681
  • 90
  • 411
  • 885
15
votes
8 answers

How do I know who holds the shared_ptr<>?

I use boost::shared_ptr in my application in C++. The memory problem is really serious, and the application takes large amount of memory. However, because I put every newed object into a shared_ptr, when the application exits, no memory leaking can…
user25749
  • 4,825
  • 14
  • 61
  • 83
14
votes
2 answers

How to remove (non-intrusive) smart pointers from a cache when there are no more references?

Because of my noob reputation, I cannot reply to this Thread, in specific the accepted answer: I never used boost::intrusive smart pointers, but if you would use shared_ptr smart pointers, you could use weak_ptr objects for your cache. Those…
dwn
  • 413
  • 3
  • 12
14
votes
4 answers

Custom allocators as alternatives to vector of smart pointers?

This question is about owning pointers, consuming pointers, smart pointers, vectors, and allocators. I am a little bit lost on my thoughts about code architecture. Furthermore, if this question has already an answer somewhere, 1. sorry, but I…
j00hi
  • 5,420
  • 3
  • 45
  • 82
14
votes
4 answers

boost shared_ptr: difference between operator= and reset?

Are there any differences between the two pieces of code below? Is any of them preferable to the other? operator= boost::shared_ptr foo; // foo.ptr should be NULL foo = boost::shared_ptr(new Blah()); // Involves creation and copy of a…
rturrado
  • 7,699
  • 6
  • 42
  • 62
14
votes
1 answer

std::unique_ptr API prohibits derived-to-base pointer conversions

In Modern Effective C++, "Iterm 19: Use std::shared_ptr for shared-ownership resource management.", Page 133-134, it says: std::shared_ptr supports derived-to-base pointer conversions that make sense for single objects, but that open holes in the…
chaosink
  • 1,329
  • 13
  • 27
14
votes
2 answers

how boost::~shared_ptr works?

when reading "Beyond the C++ Standard Library: An Introduction to Boost " ,I got a very interesting example: class A { public: virtual void sing()=0; protected: virtual ~A() {}; }; class B : public A { public: virtual…
camino
  • 10,085
  • 20
  • 64
  • 115
14
votes
1 answer

Since C++17 supports shared_ptr of array, does this mean that an explicit deleter for T[] is no longer required in both ctor and reset?

When create shared_ptr using a separated allocation, an explicit delete function must be provided in C++14 ctor and reset member function. using std::string; using std::shared_ptr; using std::default_delete; int arr_size{}; ... auto…
sandthorn
  • 2,770
  • 1
  • 15
  • 59
14
votes
3 answers

Replacing the content of the shared pointer?

I have a shared pointer that I have shared around the system. Later on, I want to replace the actual content of these shared pointers point to, but still keep all the shared pointers valid, so they internally point to this new object. Is there an…
KaiserJohaan
  • 9,028
  • 20
  • 112
  • 199
14
votes
1 answer

Is a moved-from shared_ptr guaranteed to be emptied?

Consider the following code: struct Bar { std::shared_ptr MemberFunction() { return std::move(m_memberVariable); } std::shared_ptr m_memberVariable; }; Is it guaranteed that the std::move from a shared_ptr will…
Mike Vine
  • 9,468
  • 25
  • 44
14
votes
2 answers

Temporary read-only copy of unique_ptr

I'm pretty new to C++11's smart pointers, and I'm trying to use them effectively in a project. In my project, I have a lot of functions that take a const reference to a vector of unique_ptr, do some computations on it, and place some results in a…
Edward
  • 5,942
  • 4
  • 38
  • 55
14
votes
3 answers

boost shared_from_this and multiple inheritance

I am currently having some troubles when using boost enable_shared_from_this and multiple inheritance. The scenario can be described as follows: Class A implements some functionality and should inherit from enable_shared_from_this Class B…
14
votes
2 answers

Best way to protect from multiple shared_ptr to the same object

Sending the same pointer into two different shared_ptr's is bad, it causes double deallocation, like thus: int* p = new int; std::shared_ptr p1(p); std::shared_ptr p2(p); // BAD You can accomplish the same purpose with…
Jorge Rodriguez
  • 603
  • 1
  • 6
  • 14
14
votes
2 answers

Overhead and implementation of using shared_ptr

Short introduction: I am working on multithread code and I have to share dynamically allocated objects between two threads. To make my code cleaner (and less error-prone) I want to explicitly "delete" objects in each thread and that's why I want to…
Goofy
  • 5,187
  • 5
  • 40
  • 56
13
votes
1 answer

shared, weak and lazy pointers in C++

Is anyone aware of an implementation of shared_ptr and weak_ptr together with a lazy initialization partner? The requirements of the classes were: A lazy_ptr class that allows a client to construct the object later (if at all), without needing the…
user1078210
  • 131
  • 3