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
17
votes
7 answers

Call default copy constructor from within overloaded copy constructor

I need to write a copy constructor that deep copies the contents of a std::shared_ptr. However, there are a bunch of variable int a, b, c, d, e; also defined in the class. Is there a way to generate the default copy constructor code (or call the…
Alan Turing
  • 12,223
  • 16
  • 74
  • 116
17
votes
1 answer

shared_from_this() causes std::bad_weak_ptr even when correctly using make_shared

I am creating a C++ server application using standalone Asio and C++11 and am getting an error, which is why I am asking for help. The error In the class worker_thread, during the call to shared_from_this(), a bad_weak_ptr exception is raised, which…
Petar Nikić
  • 181
  • 1
  • 5
17
votes
4 answers

`weak_ptr::expired` behavior in the dtor of the object

Consider the following code: #include #include using namespace std; class T; std::weak_ptr wptr; class T { public: T() { } ~T() { std::cout << "in dtor" << std::endl; std::cout << (wptr.expired() ?…
jnbrq -Canberk Sönmez
  • 1,790
  • 1
  • 17
  • 29
17
votes
6 answers

When would I want to construct a shared pointer from a raw pointer

Thanks to std::make_shared, I wonder, whether the constructor for std::shared_ptr, which takes a raw pointer has any value except when interfacing with legacy / library code, e.g. when storing the output of a factory. Are there other legitimate…
MikeMB
  • 20,029
  • 9
  • 57
  • 102
17
votes
1 answer

Is shared_ptr swap thread safe?

Here are some code snippets. std::shared_ptr global(new int(1)); void swapper(int x) { std::shared_ptr sp(new int(x)); global.swap(sp); } Suppose i wanted to call swapper in parallel threads. Would that be threadsafe? I am…
Johannes
  • 6,490
  • 10
  • 59
  • 108
17
votes
3 answers

Array of shared pointers to different classes

I'm trying to figure out if it is possible to create an array of shared pointers to different types. For example, something like that: vector> v; v.push_back(shared_ptr(new int)); v.push_back(shared_ptr(new…
user2487382
17
votes
5 answers

Questions on usages of shared_ptr - C++

I have few questions on the best practices of using shared_ptr. Question 1 Is copying shared_ptr cheap? Or do I need to pass it as reference to my own helper functions and return as value? Something like, void init_fields(boost::shared_ptr&…
Navaneeth K N
  • 15,295
  • 38
  • 126
  • 184
17
votes
2 answers

Passing shared_ptr to lambda by value leaks memory

I have the following code : void MyClass::onOpenModalBtnClicked() { uiManager->load(L"data/ui/testmodal.json"); std::shared_ptr modal = uiManager->getElementById("loginModal"); if(modal) { …
Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66
17
votes
1 answer

Move ownership from std::shared_ptr to std::unique_ptr

I have a class A which has a field of type std::unique_ptr: class A { public: std::unique_ptr pointer; // class body }; And somewhere in code, I'm using few std::shared_ptrs which point to the same object. Now what I'd like to achieve is to move…
Piotr Chojnacki
  • 6,837
  • 5
  • 34
  • 65
17
votes
2 answers

How to get a reference to an object having shared_ptr to it?

How to get a reference to an object having shared_ptr to it? (for a simple class T)
myWallJSON
  • 9,110
  • 22
  • 78
  • 149
17
votes
1 answer

Differences between different flavours of shared_ptr

Are there any differences between boost::shared_ptr, std::tr1::shared_ptr and the upcoming (in C++0x) std::shared_ptr? Will porting from one to another have any overhead or are they basically the same?
Motti
  • 110,860
  • 49
  • 189
  • 262
16
votes
9 answers

A Question On Smart Pointers and Their Inevitable Indeterminism

I've been extensively using smart pointers (boost::shared_ptr to be exact) in my projects for the last two years. I understand and appreciate their benefits and I generally like them a lot. But the more I use them, the more I miss the deterministic…
Ash
  • 547
  • 5
  • 13
16
votes
5 answers

Why doesn't shared_ptr permit direct assignment

So when using shared_ptr you can write: shared_ptr var(new Type()); I wonder why they didn't allow a much simpler and better (imo): shared_ptr var = new Type(); Instead to achieve such functionality you need to use…
giò
  • 3,402
  • 7
  • 27
  • 54
16
votes
5 answers

Connecting three different objects

I asked a similar question a couple of hours ago about connecting two elements of a vector. Now, I would like to make my question more general. Let's assume that we have two objects of the type double namely double d1, d2. We want the third object…
Eman
  • 165
  • 1
  • 1
  • 8