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
0
votes
3 answers

C++ bind to weak_ptr not working

I have a simple test where I am trying to bind a weak_ptr argument to a global function that takes a weak_ptr, and invokes a method if the backing pointer is still valid. This seems to work when I create a lambda with the weak pointer. It also…
kman
  • 97
  • 1
  • 8
0
votes
1 answer

Disable conversion from std::weak_ptr to std::shared_ptr

I know that I can "promote" weak_ptr to shared_ptr like that: // std::weak_ptr weak; std::shared_ptr promoted(weak); My question is: can that be prevented somehow? As an exercise I wanted to create my own veeery simple implementation of a…
Mateusz Kubuszok
  • 18,063
  • 3
  • 33
  • 53
0
votes
2 answers

Can I use shared_ptrs in this example?

I have a simple event handling system that is giving me issues. To use it I inherit from the class EventHandler. The constructor then registers each object on construction. Here is EventHandler's constructor: EventHandler::EventHandler() { …
user870130
  • 565
  • 1
  • 7
  • 15
0
votes
1 answer

How to implement a boost::unordered_map with websocketpp::connection_hdl as key?

For my application, it is much more convenient and logical to hold a map of websocketpp::connection_hdls as keys in a map, but I've found that this could be potentially dangerous since they are weak_ptrs. However, it's been claimed that a…
user1382306
0
votes
1 answer

Is calling map::count with an empty weak_ptr as argument safe?

Is it safe to call map::count on an uninitialized thus empty weak_ptr safe? I'm still very inexperienced with c++ and do not have the skills to determine this. In my application, a weak_ptr is being held as key in a map and must be found first by…
user1382306
0
votes
3 answers

What is the benefits of using weak points or when can we use weak points?

Today I read book about ARC. So there are two type points both strong and weak points. I already searched the property about them and got it. But I couldn't see or understand why we use weak point instead of strong? This is simple question. Please…
Duckchan
  • 11
  • 1
0
votes
1 answer

C++: Passing delegate to other object via std::weak_ptr

I have two classes, for instance, A and B. I would like to pass A as reference to B. class I { public: virtual void callback() = 0; }; class B { public: B(I* callback) : _callback(callback) {} private: I* _callback; }; class A : public…
slashdot
  • 570
  • 4
  • 12
0
votes
1 answer

C++11 standard decision "shared_ptr(const weak_ptr& r) Throws bad_weak_ptr"

What the heck ? (real question in bold after thereafter quotation) § 20.7.2.2.1 template explicit shared_ptr(const weak_ptr& r); 23 Requires: Y* shall be convertible to T*. 24 Effects: Constructs a shared_ptr object that shares…
v.oddou
  • 5,818
  • 3
  • 27
  • 55
0
votes
1 answer

HippoMocks With weak_ptr

Just stuck with a compilation error of the code trying to mock a method taking std::weak_ptr as argument. HippoMocks has a code to compare it when calling the method With, that doesn't get compiled. I would appreciate any help. Here is the…
0
votes
3 answers

Automatically adding and removing an object from a list

I have a class. When this class is instantiated, I want the instance added to a list. When the object is deleted, I want it removed from the list. So I give the object a shared pointer to itself. I then have a list of weak pointers to those shared…
Publius
  • 1,134
  • 2
  • 10
  • 25
-1
votes
1 answer

enable shared from this crash

I am trying to create a grid with every point pointing to another from the grid (member variable drain_), possibly itself. I wanted to use shared_ptr for this and so I would need to use a weak_ptr for drain since it can point to itself. Problem is,…
Argent
  • 33
  • 6
-1
votes
1 answer

Is it compatible to "C++ philosophy" to recode your own smart pointers

I've a little question for all C++ coders ! For you, is it compatible to "C++ philosophy" to recode your own smart pointers. Actually I use shared_ptr with weak_ptr for a project but it's complicate too much the code. I could use of course raw_ptr,…
hexa
  • 11
  • 1
-1
votes
2 answers

Binding a callbacks to an expiring shared_ptr?

I am familiar with std::shared_ptr and std::weak_ptr and know how they work. However, I would like the std::shared_ptr to emit a callback, like a boost signal. This would allow std::weak_ptr, who still refer to the deleted object, to be cleaned up…
Jack Sabbath
  • 1,140
  • 1
  • 8
  • 12
-1
votes
1 answer

C++ throw bad_weak_ptr when using shared_from_this in base class

I'm planning to implement a Thread-safe Observer Pattern. But I get a coredump when testing the code below: To run the code, just compile with: g++ --std=c++11 code.cpp Please Help Me to find the problem happend in it: #include #include…
wangjild
  • 63
  • 5
-1
votes
2 answers

about race condition of weak_ptr

1. i posted the question(About thread-safety of weak_ptr) several days ago,and I have the other related question now. If i do something like this,will introduce a race condition as g_w in above example ?(my platform is ms vs2013) std::weak_ptr
Leonhart Squall
  • 770
  • 1
  • 5
  • 15
1 2 3
15
16