Questions tagged [weak-ptr]

std::weak_ptr is a smart pointer that holds a non-owning ("weak") reference to an object that is managed by std::shared_ptr

The std::weak_ptr is a smart pointer that holds a non-owning ("weak") reference to an object that is managed by std::shared_ptr.

The class template std::weak_ptr is designed for use with and represents a non-owning reference; useful in avoiding circular references, it can also be used as a "tracking reference" and converted to a std::shared_ptr when ownership is required.

261 questions
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
22
votes
5 answers

About thread-safety of weak_ptr

std::shared_ptr g_s = std::make_shared(1); void f1() { std::shared_ptrl_s1 = g_s; // read g_s } void f2() { std::shared_ptr l_s2 = std::make_shared(3); std::thread th(f1); th.detach(); g_s = l_s2; //…
Leonhart Squall
  • 810
  • 1
  • 7
  • 15
21
votes
3 answers

Is there a way to make member function NOT callable from constructor?

I have member function (method) which uses std::enable_shared_from_this::weak_from_this() In short: weak_from_this returns weak_ptr to this. One caveat is it can't be used from constructor. If somebody would use my function from constructor of…
Korri
  • 571
  • 3
  • 10
20
votes
7 answers

Binding to a weak_ptr

Is there a way to std::bind to a std::weak_ptr? I'd like to store a "weak function" callback that automatically "disconnects" when the callee is destroyed. I know how to create a std::function using a shared_ptr: std::function
Scotty
  • 2,480
  • 2
  • 16
  • 20
19
votes
3 answers

static_pointer_cast for weak_ptr

In c++0x, there is a std::static_pointer_cast for std::shared_ptr, but there is no equivalent method for std::weak_ptr. Is this intentional, or an oversight? If an oversight, how would I define an appropriate function?
tgoodhart
  • 3,111
  • 26
  • 37
19
votes
2 answers

How can I use a std::map with std::weak_ptr as key?

How can I use std::weak_ptr as key for a std::map as shown in the following code? #include #include int main() { std::map< std::weak_ptr, bool > myMap; std::shared_ptr sharedptr(new int(5)); std::weak_ptr
user987280
  • 1,578
  • 1
  • 14
  • 24
18
votes
3 answers

Why shared_ptr's reference counting object needs to keep track of the number of weak_ptrs pointing to the object too?

Hi I am reading through this document and some other documents about C++'s shared_ptr and they all seem to suggest that apart from the number of shared_ptr pointing to the allocated object, the reference count object has to keep track of how many…
Bob Fang
  • 6,963
  • 10
  • 39
  • 72
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
15
votes
1 answer

Can an expired weak_ptr be distinguished from an uninitialized one?

For example: std::weak_ptr wp1(std::make_shared()); std::weak_ptr wp2; assert(PointsToValidOrExpiredObject(wp1)); assert(!PointsToValidOrExpiredObject(wp2)); Is such a function possible? Use case: A class's constructor takes…
dlf
  • 9,045
  • 4
  • 32
  • 58
15
votes
4 answers

What is the cyclic dependency issue with shared_ptr?

I read about shared pointers and understood how to use. But I never understood the cyclic dependency problem with shared pointers and how weak pointers are going to fix those issues. Can any one please explain this problem clearly?
kadina
  • 5,042
  • 4
  • 42
  • 83
15
votes
2 answers

How does a weak_ptr know that the shared resources has expired?

Considering the following code: #include #include using namespace std; struct MySharedStruct { int i; }; void print_value_of_i(weak_ptr weakPtr) { if (shared_ptr sp = weakPtr.lock()) {…
Samaursa
  • 16,527
  • 21
  • 89
  • 160
15
votes
4 answers

How to make a c++11 std::unordered_set of std::weak_ptr

I have a set like this: set, owner_less > > setName; It works fine. But I would like to change it to an unordered set. However, I get about six pages of errors when I do that. Any ideas how to do that? After looking…
user1404617
  • 585
  • 1
  • 5
  • 20
14
votes
2 answers

How to remove (non-intrusive) smart pointers from a cache when there are no more references?

Because of my noob reputation, I cannot reply to this Thread, in specific the accepted answer: I never used boost::intrusive smart pointers, but if you would use shared_ptr smart pointers, you could use weak_ptr objects for your cache. Those…
dwn
  • 413
  • 3
  • 12
14
votes
1 answer

Store weak pointer to self

I work with a codebase that was partially implemented by someone who was in love with overly complex solutions to simple problems (e.g. template classes with two parameters that were only ever instantiated for one pair of types). One thing she did…
James Davidoff
  • 275
  • 2
  • 6
1
2
3
17 18