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
10
votes
3 answers

Why does enable_shared_from_this embed a weak pointer instead of embedding the reference counter directly?

The enable_shared_from_this helper contains a weak pointer that is set when creating shared pointer to the object. That means there is the reference-count (allocated separately or together with the object using make_shared) and an extra weak_ptr in…
Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
10
votes
1 answer

Possible MSVC compiler bug

Given shared_ptr variable declared in condition clause of for loop and for loop body contains if/continue statement, microsoft compiler (as of version 2015) generates extra destructor call (two total) per loop iteration. This causes destruction of…
Dmitry Teslenko
  • 871
  • 1
  • 7
  • 14
10
votes
5 answers

Passing a std::shared_ptr to a function that takes a std::shared_ptr?

I have a function that needs to take shared ownership of an argument, but does not modify it. I have made the argument a shared_ptr to clearly convey this intent. template void func(std::shared_ptr ptr){} I would like…
Indigox3
  • 133
  • 7
10
votes
4 answers

Should I convert shared_ptr to weak_ptr when passed to a method?

there are already a couple of questions regarding this topic, but I am still not sure what to do: Our codebase uses shared_ptr at many places. I have to admit that we did not define ownership clearly when writing it. We have some methods like void…
Philipp
  • 11,549
  • 8
  • 66
  • 126
10
votes
4 answers

boost::shared_ptr and Inheritance

I am facing a situation in which I have a std::vector of boost::shared_ptrs of a base class. During the course of my program I need to store shared pointers to derived class objects in that vector too and at some time later in the program, need to…
Aaron S
  • 5,023
  • 4
  • 29
  • 30
10
votes
3 answers

Reasons to return reference to std::unique_ptr

I wonder if there are any legitimate reasons to return unique pointers by reference in C++, i.e. std::unique_ptr&? I've never actually seen that trick before, but the new project I've got seems to use this pattern a lot. From the first glance, it…
GreyCat
  • 16,622
  • 18
  • 74
  • 112
10
votes
2 answers

C++ shared+ptr use_count for nullptr

I have 2 shared_ptrs defined and assigned nullptr. In case 1, I'm using default constructor and in case 2, I used constructor with delete method. shared_ptr sptr2(nullptr); cout << "sptr2 use_count: " << sptr2.use_count() <<…
user2255299
  • 185
  • 7
10
votes
1 answer

Does shared_ptr still own its object when calling the deleter?

I have a std::shared_ptr with a custom deleter and in that deleter, I would like to take a temporary copy of the original std::shared_ptr. Expressed in code form: struct Foo : public std::enable_shared_from_this {}; void deleter(Foo *f) { { …
10
votes
1 answer

Assigning a shared_ptr to an offset of an array

Let's say I have a shared_ptr to an array: std::shared_ptr sp(new T[10], [](T *p) { delete[] p; }); And a method: shared_ptr ptr_at_offset(int offset) { // I want to return a shared_ptr to (sp.get() + offset) here // in a way that…
u3l
  • 3,342
  • 4
  • 34
  • 51
10
votes
2 answers

Storing an std::thread in C++11 smart pointer

In C++ 11 & above what are the advantages or disadvantages when storing an std::thread as a member of class directly like so: std::thread my_thread; As opposed to storing a std::shared_ptr or std::unique_ptr to the thread like…
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
10
votes
3 answers

How to detect if a type is shared_ptr at compile time

I want to get a templatized way of finding if a type is a shared_ptr and based on that I want to have a new specialization of a function. Example main function is, template inline void CEREAL_LOAD_FUNCTION_NAME( RelaxedJSONInputArchive &…
Mr.100
  • 141
  • 3
  • 11
10
votes
1 answer

Is there an STL comparator for std::set (or std::map) with shared_ptr keys that provides value-based lookups? What exactly does std::owner_less do?

I have a std::map with shared_ptr keys, and I need it to use the actual value (of type T, i.e. *key) for lookups, not the value of the shared pointer itself. I know I can write my own custom comparator (as I've done below), but I was wondering…
Arjun Menon
  • 374
  • 2
  • 13
10
votes
3 answers

Is there a weak_ptr equivalent to shared_from_this?

I have a class which I know will always be owned by a std::shared_ptr. However passing shared_ptr or even weak_ptr to functions and methods that don't need ownership or lifetime guarantees creates unnecessary overhead. To get around this I often…
Fibbs
  • 1,350
  • 1
  • 13
  • 23
10
votes
4 answers

Pointers vs auto_ptr vs shared_ptr

I was recently introduced to the existence of auto_ptr and shared_ptr and I have a pretty simple/naive question. I try to implement a data structure and I need to point to the children of a Node which (are more than 1 and its) number may change.…
Dimitris Leventeas
  • 1,622
  • 2
  • 16
  • 29
10
votes
3 answers

std::shared_ptr and dlopen(), avoiding undefined behavior

dlopen() is a C function used for dynamically loading shared libraries at runtime. The pattern, in case you're not familiar, is thus: Call dlopen("libpath", flag) to get a void *handle to the library Call dlsym(handle, "object_name") to get a void…