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
11
votes
2 answers

testing if a shared_ptr is NULL

I have the following code snippet: std::vector< boost::shared_ptr >::iterator it; it = returnsAnIterator(); // often, it will point to a shared_ptr that is NULL, and I want to test for that if(*it) { // do stuff } else // do other stuff Am…
Max
  • 1,295
  • 6
  • 16
  • 31
11
votes
1 answer

What Happens to a weak_ptr when Its shared_ptr is Destroyed?

It seems that a weak_ptr somehow just knows when the shared_ptr it references has been destroyed. How is that? Is there a constant link maintained or something? Take the following code for example: weak_ptr test() { shared_ptr foo{new…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
11
votes
1 answer

Using shared_ptr and glutInit causes segmentation fault

Having asked this before I tried out a lot of things and found out that the problem has to do with glutInit. Take the following code examples: main.cpp #include #include #include using namespace std; int main(int…
yonarw
  • 121
  • 7
11
votes
2 answers

Find a value in a set of shared_ptr

I have a set of shared_ptr and would like to find a value in it: typedef std::shared_ptr IntPtr; struct Compare { bool operator() (const IntPtr& a, const IntPtr& b) { return *a < *b; } }; std::set s; auto x =…
Deqing
  • 14,098
  • 15
  • 84
  • 131
11
votes
2 answers

Is it thread safe to reset and copy shared_ptr simultaneously?

Boost documentation describes shared pointer's behavior when accessing it from multiple threads simultaneously. Particularly they give some examples: shared_ptr p(new int(42)); //--- Example 1 --- // thread A shared_ptr p2(p); // reads…
Mihran Hovsepyan
  • 10,810
  • 14
  • 61
  • 111
11
votes
2 answers

Should I call reset on a weak_ptr if I happen to notice it's expired?

I have a collection of Creature objects that are created and owned in one part of my application using std::make_shared and std::shared_ptr. I also keep track of a selection of zero or one Creature in a World object using a…
slajoie
  • 440
  • 8
  • 20
11
votes
2 answers

Set shared_ptr to point existing object

For the code below, I would like to know how to set std::shared_ptr to point the given objects in the two member functions. The Vector3 object which is allocated in the main function is not going to be deleted until the end of the program. #include…
Shibli
  • 5,879
  • 13
  • 62
  • 126
11
votes
3 answers

Smart pointers + cycles + "->"

Sometimes I'm really sure that I want to have circular dependence of pointers, and every object on cycle should be able to use his pointer (so it can't be weak_ptr). My question is: Does this mean that I have bad design? What if I want to implement…
Krzysztof Stanisławek
  • 1,267
  • 4
  • 13
  • 27
11
votes
4 answers

Should I use unique_ptr or shared_ptr in this case?

in the mainwindow of my QT app, I use a std::shared_ptr to hold a pointer to an instance of my network service which manages all the connections to multiple clients. Now, I have to pass this pointer to multiple sub windows so that they can…
user66875
  • 2,772
  • 3
  • 29
  • 55
11
votes
4 answers

shared_ptr to shared_ptr and vector to vector

I'm trying to define a good design for my software which implies being careful about read/write access to some variables. Here I simplified the program for the discussion. Hopefully this will be also helpful to others. :-) Let's say we have a class…
Hiura
  • 3,500
  • 2
  • 20
  • 39
11
votes
3 answers

About shared_ptr and pointer to member operator `->*` and `std::bind`

Recently I discovered that shared_ptr does not have pointer to member operator ->*. I created simple example: template auto invoke1(Pointer p, Function f, Args... args) ->…
PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
11
votes
7 answers

Shared pointers and the performance

I have been using shared pointers for soem time now, and I have performance issues in my program... So I'd like to know if shared pointers lead to performance decrease. If so, then how hard? Thanks alot. My program is multi-threaded, using…
Guest
  • 111
  • 1
  • 5
11
votes
1 answer

c++11 std::shared_ptr + boost::serialization

Hi has anybody already managed to serialize a C++11 std::shared_ptr with boost::serialization. There are lots of obsolete posts out there but none with an acceptable solution. I am not going into discussion why I want to use the std::shared_ptr just…
denim
  • 1,329
  • 1
  • 15
  • 24
11
votes
3 answers

Missing equality between shared_ptr and weak_ptr

While I do understand why there is no operator== for shared_ptr and unique_ptr, I wonder why there is none for shared_ptr and weak_ptr. Especially since you can create a weak_ptr via a reference on shared_ptr. I would assume that for 99% of the time…
abergmeier
  • 13,224
  • 13
  • 64
  • 120
10
votes
1 answer

How can I change the pointer in shared_ptr without losing the ability to delete the memory?

I faced a problem with std::shared_ptr. I have a code: void Receive(const std::shared_ptr& data, uint32_t length) { this->next->Receive(data + 2, length - 2); } I need to increment shared_ptr by 2 without losing the ability to delete the…
zenno2
  • 425
  • 2
  • 7