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

How std::enable_shared_from_this::shared_from_this works

I just cannot understand how std::enable_shared_from_this::shared_from_this returns a shared pinter that shared ownership with existing pointer. In other words you do this: std::shared_ptr getFoo() { return shared_from_this(); } So when you…
Narek
  • 38,779
  • 79
  • 233
  • 389
25
votes
4 answers

shared_ptr with non-pointer resources

In C++11 is it possible to use shared_ptr to control non-pointer resources? It is possible to use unique_ptr to manage non-pointer resources. This is done by implementing a custom deleter class which provides: A typedef {TYPE} pointer; where…
John Dibling
  • 99,718
  • 31
  • 186
  • 324
24
votes
7 answers

How can boost::serialization be used with std::shared_ptr from C++11?

I know that there is a Boost module for serialization of boost::shared_ptr, but I cannot find anything for std::shared_ptr. Also, I don't know how to implement it easily. I'm afraid that the following code namespace boost{namespace…
fiktor
  • 1,303
  • 2
  • 11
  • 22
24
votes
13 answers

How to detect cycles when using shared_ptr

shared_ptr is a reference counting smart pointer in the Boost library. The problem with reference counting is that it cannot dispose of cycles. I am wondering how one would go about solving this in C++. Please no suggestions like: "don't make…
Unknown
  • 45,913
  • 27
  • 138
  • 182
24
votes
4 answers

C++ shared_ptr equality operator

The equality operator for shared_ptr's is defined as follows: template inline bool operator==( shared_ptr const & a, shared_ptr const & b) { return a.get() == b.get(); } This seems broken. Would it not have been…
user231536
  • 2,661
  • 4
  • 33
  • 45
24
votes
2 answers

Is there a recommended way to test if a smart pointer is null?

I'm trying to check if a std::shared_ptr is null. Is there a difference between doing std::shared_ptr p; if (!p) { // method 1 } if (p == nullptr) { // method 2 }
Sidd
  • 1,168
  • 2
  • 10
  • 27
24
votes
2 answers

weak_ptr, make_shared and memory deallocation

A control block of a shared_ptr is kept alive while there is at least one weak_ptr present. If the shared pointer was created with make_shared that implies that the whole memory of the object is kept allocated. (The object itself is properly…
Ilya Popov
  • 3,765
  • 1
  • 17
  • 30
23
votes
3 answers

Is a shared_ptr's deleter stored in memory allocated by the custom allocator?

Say I have a shared_ptr with a custom allocator and a custom deleter. I can't find anything in the standard that talks about where the deleter should be stored: it doesn't say that the custom allocator will be used for the deleter's memory, and it…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
23
votes
2 answers

Shared void pointers. Why does this work?

To solve a very peculiar problem in my application I need a shared-pointer to allocated data, but to the outside world, the underlying data type should remain hidden. I could solve this by making some kind of Root class of which all my other classes…
Patrick
  • 23,217
  • 12
  • 67
  • 130
23
votes
3 answers

Create shared_ptr from reference

I'm relativly new to C++ and this seems like a noob question but I wasn't able to solve it with other resources on the internet. I'm trying to create a shared_ptr from a reference. I have the following Book class: #include #include…
Cilenco
  • 6,951
  • 17
  • 72
  • 152
23
votes
4 answers

std::enable_shared_from_this; public vs private

I was playing around for a bit using the shared_ptr's and enable_shared_from_this, while I run into something I don't really understand. In my first attempt I constructed something like this: class shared_test :…
23
votes
3 answers

shared_ptr with malloc and free

I have working in large application which contain c and cpp. The all files saved as cpp extension but the code is written in c- style. I mean it is define structure rather than class allocate memory through malloc and realloc and calloc.In recent…
user765443
22
votes
2 answers

If I want to use std::shared_ptr, which header to include?

In c++0x shared_ptr will be moved from tr1 into std. So which header to include to get it? I am using g++ 4.5 (ubuntu 10.10)
BЈовић
  • 62,405
  • 41
  • 173
  • 273
22
votes
3 answers

When should we use std::enable_shared_from_this

I just knew std::enable_shared_from_this form this link. But after reading the code below, I don't know when to use it. try { Good not_so_good; std::shared_ptr gp1 = not_so_good.getptr(); } catch(std::bad_weak_ptr& e) { …
Yves
  • 11,597
  • 17
  • 83
  • 180
22
votes
1 answer

memory leak under GCC (but not Clang) when throwing in the middle of a C++14 initializer list for std::list

Consider the following program: #include #include #include #include class Foo { public: Foo(){ if (s_ct==0) {throw std::bad_alloc();} --s_ct; fprintf(stderr, "ctor %p\n", this); …
Eric
  • 223
  • 1
  • 4