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

Segmentation fault when using a shared_ptr for private_key

Updates [X] I discovered this happen when TLS::credentials creds is declared on global scope but if I declare it outside seg fault won't happen. I need it to be global because it helps with caching certificates and that multiple threads can use…
jeffbRTC
  • 1,941
  • 10
  • 29
51
votes
1 answer

Why shared_from_this can't be used in constructor from technical standpoint?

In the book The C++ Standard Library at page 91 I have read this about shared_from_this(): The problem is that shared_ptr stores itself in a private member of Person’s base class, enable_shared_from_this<>, at the end of the construction of the…
Yola
  • 18,496
  • 11
  • 65
  • 106
51
votes
5 answers

Are there any downsides with using make_shared to create a shared_ptr

Are there any downsides with using make_shared() instead of using shared_ptr(new T). Boost documentation states There have been repeated requests from users for a factory function that creates an object of a given type and returns a…
Tobias Furuholm
  • 4,727
  • 4
  • 30
  • 39
49
votes
13 answers

What are potential dangers when using boost::shared_ptr?

What are some ways you can shoot yourself in the foot when using boost::shared_ptr? In other words, what pitfalls do I have to avoid when I use boost::shared_ptr?
Frank
  • 64,140
  • 93
  • 237
  • 324
48
votes
5 answers

How do shared pointers work?

How do shared pointers know how many pointers point to that object? (shared_ptr, in this case)
cam
  • 8,725
  • 18
  • 57
  • 81
48
votes
6 answers

C++ smart pointer const correctness

I have a few containers in a class, for example, vector or map which contain shared_ptr's to objects living on the heap. For example template class MyExample { public: private: vector > vec_; map,…
user231536
  • 2,661
  • 4
  • 33
  • 45
47
votes
1 answer

Cohabitation of boost::shared_ptr and std::shared_ptr

I want to use boost::log at some point, but I cannot pass a std::shared_ptr as a parameter, because the compiler (VS2010) cannot convert it into a boost::shared_ptr. I don't really like the fact that they are aliens to one another. Is there a safe…
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
46
votes
13 answers

What (not) to do in a constructor

I want to ask you for your best practices regarding constructors in C++. I am not quite sure what I should do in a constructor and what not. Should I only use it for attribute initializations, calling parent constructors etc.? Or might I even put…
tyrondis
  • 3,364
  • 7
  • 32
  • 56
46
votes
4 answers

boost, shared ptr Vs weak ptr? Which to use when?

In my current project I am using boost::shared_ptr quite extensively. Recently my fellow team mates have also started using weak_ptr. I don't know which one to use and when. Apart from this, what should I do if I want to convert weak_ptr to…
RLT
  • 4,219
  • 4
  • 37
  • 91
46
votes
2 answers

Problems using member function as custom deleter with std::shared_ptr

I'm trying to work out how to use std::shared_ptr with a custom deleter. Specifically, I'm using it with SDL_Surface as: std::shared_ptr(SDL_LoadBMP(....),SDL_FreeSurface); which compiles and runs fine. However, I would like to try out…
Wheels2050
  • 879
  • 2
  • 9
  • 17
45
votes
4 answers

How to intentionally delete a boost::shared_ptr?

I have many boost::shared_ptr objects, and at some point I intentionally want to delete some of them to free some memory. (I know at that point that I will never need the pointed-to MyClass objects anymore.) How can I do that? I guess you…
Frank
  • 64,140
  • 93
  • 237
  • 324
44
votes
3 answers

Alternatives of static_pointer_cast for unique_ptr

I understand that using static_pointer_cast with unique_ptr would lead to a shared ownership of the contained data. In other terms, what I'd like to do is: unique_ptr foo = fooFactory(); // do something for a while unique_ptr bar =…
skypjack
  • 49,335
  • 19
  • 95
  • 187
44
votes
3 answers

Does using .reset() on a std::shared_ptr delete all instances

I'm new to shared_ptr's and I'm trying to figure out the exact functionality of the .reset() function. #include #include using namespace std; class SomeClass{}; int main() { shared_ptr sp (nullptr); //do some…
zeus_masta_funk
  • 1,388
  • 2
  • 11
  • 34
44
votes
1 answer

Equality-compare std::weak_ptr

I want to compare two std::weak_ptr's or one std::weak_ptr and one std::shared_ptr for equality. What I want to know is whether the object each of the weak_ptr's/shared_ptr's point to is the same. The comparison should yield negative results not…
fat-lobyte
  • 1,129
  • 1
  • 10
  • 16