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
0
votes
1 answer

Vector of weak_ptr, lock(). Lower bound. Segfault

I'm using lower_bound() for searching in sorted vector of weak_ptr vector >::iterator findA( const string & id ) const { sa = make_shared( id ); a = sa; return lower_bound( owners.begin(), owners.end(), sa, []…
0
votes
2 answers

Can I make a shared object pool using std::shared_ptr and weak_ptr without a custom destructor?

I want to have a pool of shared objects whose class name is Shader. I want a "client" to be able to request a Shader with certain parameters from the pool manager, and if there's one already in the pool the manager will return a pointer or reference…
realh
  • 962
  • 2
  • 7
  • 22
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
  • 107
  • 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
  • 24,995
  • 4
  • 42
  • 64
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
  • 8
  • 16
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++ 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
  • 73
  • 5
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
  • 630
  • 5
  • 14
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
  • 6,476
  • 3
  • 32
  • 63
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,184
  • 2
  • 10
  • 27
-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 2 3
17
18