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
1
vote
0 answers

When should I use weak_ptr?

I know that weak_ptr is a good way to avoid cyclic references. However I'm not sure whether I should use it to improve performance when possible, for example when just reading the content in the pointer. E.g. I have a vector storing smart pointers:…
Deqing
  • 14,098
  • 15
  • 84
  • 131
1
vote
1 answer

std::weak_ptr.lock semantic in multi-threaded c++

std says: shared_ptr lock() const noexcept; Returns: expired() ? shared_ptr() : shared_ptr(*this). but in between expired returning false (the object still exists) and the construction of the shared_ptr another thread could remove the…
Klaus Ahrens
  • 229
  • 3
  • 7
1
vote
1 answer

Same address, multiple "shared_ptr"s, enable_shared_from_this & custom deleter

Related to Same address, multiple shared_ptr counters, is it forbidden by C++ standard? and myriad other questions around multiple shared_ptr objects pointing to the same object but not sharing the underlying ref count structure. What happens if the…
zrb
  • 721
  • 1
  • 6
  • 15
1
vote
3 answers

How can I do this with Smart Pointers?

Here's what I'm trying to achieve: #include using std::cout; #include using std::vector; int main() { vector a {3, 7}; int *p = &a.at (0); //assign 3 for (int i = 0; i < 10; ++i) //swap and print (3,7,3...) …
chris
  • 60,560
  • 13
  • 143
  • 205
0
votes
2 answers

c++: std::tr1::shared_ptr from this

I have the following code: #include class Foo; typedef std::tr1::shared_ptr pFoo_t; class DoSomething { public: static void doSomething( pFoo_t p) { printf( "doing something...\n"); } static void doSomethingElse( pFoo_t p) {…
Swan911
  • 49
  • 1
  • 7
0
votes
0 answers

Why is the weak reference count of a std::shared_ptr not zero after std::make_shared invoked?

After executing lines 40 and 41 below, the debugger is telling me that the weak reference count for the std::shared_ptr's a and b is 2. The ctor for Value is not storing any strong or weak references to these objects: struct Value : public…
wcochran
  • 10,089
  • 6
  • 61
  • 69
0
votes
2 answers

'std::bad_weak_ptr' error while using shared_from_this

Note: Before posting the question, I have gone through the existing questions on std::bad_weak_error while using shared_from_this to pass the shared_ptr of the existing shared_ptr instance to another method. None of them are similar to this…
LKB
  • 127
  • 2
  • 8
0
votes
0 answers

Can a unique_ptr be moved when referenced by a weak_ptr?

I have a bunch of unique_ptrs for database connections. They get checked out by different parts of the code to run database queries, then returned (std::move) back to the container for next use. I want to keep track of all instances of these…
Sean
  • 1
0
votes
3 answers

Make do without weak_ptr

I found an example here , about using an observer thread with an weak pointer: std::thread observer; void observe(std::weak_ptr wp) { //Start observer thread observer = std::thread([wp](){ while(true) { …
0
votes
0 answers

How to compute hash of aliased std::weak_ptr?

Assuming a custom HashableWeakPointer as proposed in this question I would like to ask for further clarification of statements written in an answer to that question. template struct HashableWeakPointer { // weak ptr API clone goes here. …
0
votes
2 answers

How to prove c++11 make_shared() can keep shared_ptr's control block alive even after its dtor?

My question is: is there a case that share_ptr ref count is 0, while weak_ptr ref count is not 0? Difference in make_shared and normal shared_ptr in C++ Referr this thread, showing that if a shared_ptr is created by make_shared and there's weak_ptr,…
Troskyvs
  • 7,537
  • 7
  • 47
  • 115
0
votes
0 answers

ControlBlock member of weak_ptr

So I've been reading about shared_ptr/weak_ptr implementation and I do not get a small detail. As far as I understand, there is a class ControlBlock defined in the private section of shared_ptr. But weak_ptr also stores a pointer to some…
0
votes
2 answers

Capture shared_ptr in lambda

I want to capture a shared_ptr in my lambda expression. Tried two methods: Capture the shared pointer error: invalid use of non-static data member A::ptr Create a weak pointer and capture it (Found this via some results online). I'm not sure if…
Friday
  • 23
  • 1
  • 5
0
votes
0 answers

Dangling pointers... Is there a another solution than std::weak_ptr?

This is more a disussion than a question, but still.. A few days ago I found myself having a big problem in my program. I'll try and be brief with the situation I encountered. My plan was to have a callback function being executed every time a…
0
votes
2 answers

weak_ptr to singleton not thread-safe

I'm writing a function that returns a shared_ptr to a singleton. I want the singleton object to be destroyed when all of the references have gone away. My solution builds on this accepted answer which uses a static weak_ptr and mutex, but my test…
samw
  • 79
  • 1
  • 7