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

How do I implement polymorphism with std::shared_ptr?

I have seen some of the other questions on this topic, but have still not found the answer - I guess I'm missing something: I defined two simple test classes: class TestBase { public: TestBase ( ) { }; ~ TestBase ( ) { }; protected: …
Vector
  • 10,879
  • 12
  • 61
  • 101
27
votes
3 answers

C++: "... is not a polymorphic type" while using boost::dynamic_pointer_cast

Why do I receive the following error for the following code? 1>C:\Libs\boost_1_44\boost/smart_ptr/shared_ptr.hpp(259): error C2683: 'dynamic_cast' : 'my_namespace::A' is not a polymorphic type 1> D:\[location]\[header_filename].h(35) : see…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
27
votes
1 answer

Is it correct to return null shared_ptr?

For example, there is a function that finds an object and returns shared_ptr if object is found, and must indicate somehow that no object was found. std::vector Storage::objects; std::shared_ptr Storage::findObject() { if…
John Lock
  • 623
  • 1
  • 7
  • 18
26
votes
5 answers

Overload method for unique_ptr and shared_ptr is ambiguous with polymorphism

Coding stuff after taking the hint from my previous question's answer, I ran into an issue with overloading Scene::addObject. To reiterate the relevant bits and make this self contained, with the least details possible: I have a hierarchy of…
aPonza
  • 492
  • 4
  • 10
26
votes
1 answer

Can I use std::make_shared with structs that don't have a parametric constructor?

Say I have a struct like this: struct S { int i; double d; std::string s; }; Can I do this? std::make_shared(1, 2.1, "Hello")
Narek
  • 38,779
  • 79
  • 233
  • 389
26
votes
3 answers

Custom (pool) allocator with boost shared_ptr

I want objects managed by a shared_ptr to be allocated from a pool, say Boost's Pool interface, how can this be achieved?
myahya
  • 3,079
  • 7
  • 38
  • 51
26
votes
3 answers

What is the meaning of this piece of Standardese about shared_ptr's use_count()?

While trying to wrap my head around the problem shown in this question I found myself stuck on the following sentence from [util.smartptr.shared]/4: [...] Changes in use_count() do not reflect modifications that can introduce data races. I don't…
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
26
votes
1 answer

std::unique_ptr vs std::shared_ptr vs std::weak_ptr vs std::auto_ptr vs raw pointers

What are the equivalent uses of each smart pointer in comparison to similar (but not limited to) some advanced techniques using raw pointers? My understanding is minimal, but from what I can gather: Raw Pointers: Only use if you really, really,…
Casey
  • 10,297
  • 11
  • 59
  • 88
26
votes
1 answer

make_shared and emplace functions

I was trying to find some easy way to emplace elements in a std::vector> but couldn't come with anything. std::shared_ptr takes pointers as parameters, so I can still write this: std::vector>…
Morwenn
  • 21,684
  • 12
  • 93
  • 152
26
votes
4 answers

Using shared_ptr in dll-interfaces

I have an abstract class in my dll. class IBase { protected: virtual ~IBase() = 0; public: virtual void f() = 0; }; I want to get IBase in my exe-file which loads dll. First way is to create following function IBase *…
Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
26
votes
6 answers

What is boost's shared_ptr(shared_ptr const & r, T * p) used for?

boost::shared_ptr has an unusual constructor template shared_ptr(shared_ptr const & r, T * p); and I am a little puzzled as to what this would be useful for. Basically it shares ownership with r, but .get() will return p. not…
Evan Teran
  • 87,561
  • 32
  • 179
  • 238
26
votes
1 answer

Why doesn't std::weak_ptr<> provide a bool conversion?

C++11's std::shared_ptr<> provides a kind of bool operator. operator unspecified-bool-type() const; (It's not a straight-up operator bool() const due to the dangers from implicit casting of type bool.) Why doesn't std::weak_ptr<> have a similar…
OldPeculier
  • 11,049
  • 13
  • 50
  • 76
25
votes
5 answers

questions regarding shared_from_this

I have a function which takes a shared_ptr. In some member function memfun of MyClass, I need to pass this to that function. But if I write void MyClass:memfun() { func(shared_ptr(this)) } I am assuming that after the call has…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
25
votes
4 answers

Garbage collection vs. shared pointers

What are the differences between shared pointers (such as boost::shared_ptr or the new std::shared_ptr) and garbage collection methods (such as those implemented in Java or C#)? The way I understand it, shared pointers keep track of how many times…
helloworld922
  • 10,801
  • 5
  • 48
  • 85
25
votes
2 answers

Why does shared_ptr::use_count() return a long instead of an unsigned type?

shared_ptr observers 20.8.2.2.5 C++14 Final Draft (n4296) long use_count() const noexcept; Returns: the number of shared_ptr objects, *this included, that share ownership with *this, or 0 when *this is empty. [Note: use_count() is not…
ricky m
  • 396
  • 2
  • 10