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.

228 questions
292
votes
14 answers

When is std::weak_ptr useful?

I started studying smart pointers of C++11 and I don't see any useful use of std::weak_ptr. Can someone tell me when std::weak_ptr is useful/necessary?
user1434698
76
votes
5 answers

shared_ptr and weak_ptr differences

I am reading Scott Meyers "Effective C++" book. It was mentioned that there are tr1::shared_ptr and tr1::weak_ptr act like built-in pointers, but they keep track of how many tr1::shared_ptrs point to an object. This is known as reference counting.…
venkysmarty
  • 10,013
  • 19
  • 87
  • 165
67
votes
2 answers

How does weak_ptr work?

I understand how to use weak_ptr and shared_ptr. I understand how shared_ptr works, by counting the number of references in its object. How does weak_ptr work? I tried reading through the boost source code, and I'm not familiar enough with boost to…
Oliver Zheng
  • 6,973
  • 8
  • 49
  • 59
65
votes
7 answers

Why can't a weak_ptr be constructed from a unique_ptr?

If I understand correctly, a weak_ptr doesn't increment the reference count of the managed object, therefore it doesn't represent ownership. It simply lets you access an object, the lifetime of which is managed by someone else. So I don't really see…
notadam
  • 2,494
  • 2
  • 16
  • 30
44
votes
4 answers

boost, shared ptr Vs weak ptr? Which to use when?

In my current project I am using boost::shared_ptr quite extensively. Recently my fellow team mates have also started using weak_ptr. I don't know which one to use and when. Apart from this, what should I do if I want to convert weak_ptr to…
RLT
  • 4,073
  • 3
  • 33
  • 87
36
votes
2 answers

What's the difference between raw pointer and weak_ptr?

As in title. This question probably already has an answer but I failed to find one.
NPS
  • 5,261
  • 8
  • 46
  • 80
35
votes
1 answer

Equality-compare std::weak_ptr

I want to compare two std::weak_ptr's or one std::weak_ptr and one std::shared_ptr for equality. What I want to know is whether the object each of the weak_ptr's/shared_ptr's point to is the same. The comparison should yield negative results not…
fat-lobyte
  • 1,029
  • 1
  • 10
  • 16
32
votes
2 answers

Trivial cases of shared_ptr and weak_ptr failing

I'm having trouble using shared_ptr and weak_ptr along with enable_shared_from_this. When I google the symptoms of what I'm seeing, everybody suggests "you cannot use shared_from_this() when there are no shared_ptr instances owning your object. But…
28
votes
3 answers

What's the performance penalty of weak_ptr?

I'm currently designing a object structure for a game, and the most natural organization in my case became a tree. Being a great fan of smart pointers I use shared_ptr's exclusively. However, in this case, the children in the tree will need access…
Kornel Kisielewicz
  • 51,225
  • 12
  • 100
  • 147
25
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
  • 8,686
  • 9
  • 52
  • 79
25
votes
2 answers

why i can't cast nullptr to weak_ptr<>

class MyClass { public: MyClass(std::weak_ptr parent){} } i want to do this: auto newInstance = std::make_shared(nullptr); or default value of weak_ptr argument is null, such as : void function(int arg,std::weak_ptr
uray
  • 10,130
  • 9
  • 46
  • 73
22
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
  • 9,811
  • 8
  • 42
  • 71
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
  • 531
  • 3
  • 10
21
votes
5 answers

What is diffrence between lock() and expired()? weak_ptr C++

Recently I started at C++11. I studied about weak_ptr. There exist two ways of getting raw pointer. lock() function shared_ptr spFoo = wpPtr.lock(); if(spFoo) { spFoo->DoSomething(); } expired() function if(!wpPtr.expired()) { …
zeno
  • 233
  • 1
  • 2
  • 7
21
votes
4 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
  • 770
  • 1
  • 5
  • 15
1
2 3
15 16