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
252
questions
0
votes
3
answers
106
views
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<int> wp) {
//Start observer thread
observer = std::thread([wp]()...
0
votes
0
answers
47
views
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<class T>
struct ...
1
vote
0
answers
61
views
What's the use-case that, 2 weak_ptr share ownership while raw pointer are different? [duplicate]
I'm looking at cppreference for std::owner_less:
It says:
The order is such that two smart pointers compare equivalent only if they are both empty or if they share ownership, even if the values of ...
0
votes
2
answers
89
views
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 ...
0
votes
0
answers
31
views
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 ...
-2
votes
1
answer
55
views
How to not allow conversion from temporary shared_ptr to weak_ptr for derived types
I have asked this question for concrete types. The provided solution is sufficient for those, but when it comes to inheritance, it fails. Would there be a solution to that as well?
Lets have a ...
5
votes
1
answer
120
views
How to ensure a weak_ptr is not created from a temporary shared_ptr?
Let's have class Foo and method void use_weak_ptr(std::weak_ptr<Foo>). Is there a way to ensure - preferably at compile time - that the method is not called with temporary?
Allow this:
auto ...
0
votes
0
answers
23
views
Why shared_from_this() in variadic template class cast to weak_ptr will crash when running?
I want to cast a shared_from_this() ptr to weak_ptr, but will crash. I don't know why.
The minimal code is under bellow:
#include <memory>
#include <iostream>
class Base: public std::...
0
votes
2
answers
437
views
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 ...
0
votes
0
answers
24
views
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 ...
0
votes
2
answers
97
views
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 ...
5
votes
2
answers
207
views
Can I reset shared_ptr without deleting object so that weak_ptr loses a reference to it
I'd like to reset a shared_ptr without deleting its object and let weak_ptr of it loses a reference to it. However, shared_ptr doesn't have release() member function for reasons, so I can't directly ...
1
vote
1
answer
217
views
About shared_mutex and shared_ptr across multiple threads
I implemented code such that multiple instances running on different threads reads other instances' data using reader-writer lock and shared_ptr. It seemed fine, but I am not 100% sure about that and ...
2
votes
1
answer
156
views
Calling `.lock()` on weak_ptr returns NULL shared_ptr
I am somewhat confused by the behaviour of the .lock() call on a weak_ptr. My understanding is that .lock() will return a shared_ptr of the relevant type if it has not expired otherwise it will be a ...
0
votes
1
answer
2k
views
operator -> or ->* applied to "const std::weak_ptr" instead of to a pointer typeC/C++
In a lambda function, instead of this, I was trying to use weak_ptr to access all member function and variable, but I'm getting this error:
operator -> or ->* applied to "const std::...
3
votes
1
answer
126
views
how to implement std::weak_ptr::lock with atomic operations?
I recently tried to implement an atomic reference counter in C, so I referred to the implementation of std::shared_ptr in STL, and I am very confused about the implementation of weak_ptr::lock.
When ...
4
votes
2
answers
235
views
How to compute hash of std::weak_ptr?
So I have code that uses std::weak_ptr and maintains them in an std::set, and that works just fine -- and has worked for the last 5 or 7 years. Recently I thought I'd fiddle with using them in an std::...
2
votes
1
answer
134
views
Swig: simple idiomatic wrapper usage when weak_ptr are used?
note: this question is related to weak_ptr usage, but is not about wrapping weak_ptrs.
I am currently evaluating Swig and I have found an "inconvenience" in the usage of the wrappers by the ...
1
vote
1
answer
77
views
Why does std::weak_ptr<T>::lock return empty shared pointer here?
I'm trying to create an AABBTree structure where each node knows its parent and children.
Within my AABBTreeNode class, the parent is stored as a std::shared_ptr<AABBTreeNode> and the children ...
0
votes
1
answer
238
views
Calling a function on weak pointer
I have written the following code:
#include <iostream>
#include <memory>
using namespace std;
class Foo
{
int x = 0;
public:
int get() const { return x; }
void set(int x) { ...
3
votes
4
answers
272
views
Mutual dependency of two objects
Quite frequently, I stuble over a situation like this: two objects need to know each other, and we have a mutual aggregation-style dependency (imagine, for example, one object handles a websocket ...
2
votes
1
answer
333
views
std::weak_ptr::lock and object destruction
I know that in a multi-threading environment it's not safe to check whether an object has been fully deleted by std::shared_ptr<T>::use_count() == 0, because the object's destructor may still be ...
0
votes
1
answer
72
views
How to fix this shared_ptr reference cycles?
I designed an App that holds a stack of layers and an active obj.
When a Layer is attached to the App, the Layer tells App what an active object is. But my design causes a sigtrap when deallocating.
...
0
votes
1
answer
698
views
How to access the class member functions from a weak pointer in C++?
I am a complete newbie to smart pointers, and I have never dealt with weak_ptr in C++.
I have a function in class Y, that takes in as parameter a weak_ptr of class X.
Inside the function in class Y, I ...
3
votes
1
answer
614
views
How does the ::lock() function work on std::weak_ptr?
When you have a weak_ptr, and you do:
std::weak_ptr<int> wp; // Pretend it's assigned to a shared_ptr
if (!wp.expired()) // Equivalent to use_count() != 0
{ /* Here we're not 100% sure the raw ...
0
votes
0
answers
121
views
what is the lifetime of a shared pointer inside a constructor of an instance?
my first question:
If I create a shared_ptr<class A> inside of class B constructor:
class B{
public:
B(){
auto a = make_shared<A>();
}
};
will the owner of the object A be ...
2
votes
1
answer
578
views
What are best use cases of shared_ptr, unique_ptr and weak_ptr?
While it is easy to learn about std::shared_ptr, std::unique_ptr and std::weak_ptr by knowing what are they, it seems to be very difficult for me to understand in what circumstances they proved to be ...
2
votes
2
answers
481
views
How to give a child a weak pointer to a parent? (C++)
I've read here that, in a situation where some parent object uniquely owns several children, and each child needs to be able to access its parent, a weak pointer should be used as the back-pointer ...
2
votes
1
answer
89
views
Usecase for weak_ptr in C++ primer 5th Edition
Hi i am reading C++ primer 5th addition and have some doubts in the section of weak_ptr. It is written that
By using a weak_ptr, we don’t affect the lifetime of the vector to which a given StrBlob ...
1
vote
1
answer
52
views
C++: How to achieve unique ownership but non-unique thread-safe weak access
I have a class Inner that should be uniquely owned by class Outer, specifically, nobody but Outer should ever be able to destroy Inner, or keep it alive.
However, I want other classes Consumers, ...
5
votes
2
answers
321
views
Understanding of Scott Meyers' third example of std::weak_ptr
The final example at page 137 of Effective Modern C++ draws the scenario of a data structure with objects A, B, and C in it, connected to each other via std::shared_ptr in the following way:
std::...
0
votes
0
answers
73
views
How to get access to private members of other class?
I am implementing my own SharedPtr and WeakPtr and there are many methods where I need access to private members of SharedPtr from WeakPtr's methods. For example:
template<typename Resource>
...
1
vote
2
answers
151
views
Why am I getting segmentational fault with weak_ptr
I am doing an exercise to get to know the shared pointer and weak pointer.
So the example is with cyclic dependence of shared_ptr and how could I solve the problem with weak_ptr.
I want to initialize ...
0
votes
1
answer
223
views
I can't understand weak pointers (C++) [closed]
I am trying to learn a bit about std::weak_ptr(semantics and usecases), however I cannot find many resources online. Cppreference didn't help at much, and there are a few other sites, but they didn't ...
0
votes
0
answers
85
views
Is there a C++ container that can handle changing hash/comparison/equality methods?
I have a class managed by std::shared_ptr. This class has a hash and all the ==, <, etc. operators. For simplicity let's say that class is int. What I want is a registry of all int's currently in ...
2
votes
1
answer
102
views
Use of weak_ptr with cyclic references
So I'm having quite a hard time understanding why we have to use weak_ptr especially with cyclic reference problems, consider this code:
class B; //forward declaration
class A {
shared_ptr<B&...
1
vote
1
answer
54
views
Give container ownership of its children, but have children store a reference to their parent using smart pointers
I would like to have all the children of a Node in a tree to be owned by their parent, and have each child store a reference to its parent. That way, when a parent Node is destroyed, all of its ...
3
votes
2
answers
460
views
How can I use std::enable_shared_from_this in both super and subclass?
I have two classes A and B, where B is a subclass of A. I need both classes to use std::enable_shared_from_this.
I tried this:
#include <memory>
#include <iostream>
#include <vector&...
2
votes
1
answer
465
views
weak_ptr obtained from weak_from_this() is invalid
Here is a minimal example to reproduce:
source code:
#include <memory>
#include <iostream>
class A : public std::enable_shared_from_this<A>
{
public:
A(std::weak_ptr<A> ...
3
votes
2
answers
2k
views
weak_ptr, shared_ptr in lambda capture list and std::queue<T,Container>::emplace
I have a function which enqueue lambda using std::queue's build-in emplace. I created shared_ptr object (task), which later I would capture in lambda.
template<typename Func, typename... Args&...
0
votes
0
answers
82
views
Is it possible to store shared_ptr and weak_ptr in one container?
It seems that I'm missing something obvious, but I can't find an elegant solution of the following problem.
I need to create a collection of objects, where some of them are pointed by shared_ptr (and ...
1
vote
1
answer
751
views
Storing of std::weak_ptr<void> and using static_pointer_cast
Will the reference count still work if storing std::weak_ptr with static_pointer_cast?
Here is a very simplifed example (note that the SmallBox and BigBox classes are almost exactly the same):
#...
8
votes
1
answer
866
views
Where is the race in this thread sanitzer warning?
The below code produce a warning when running with thread sanitizer on macOS. I can't see where the race is. The control block of shared_ptr and weak_ptr is thread safe, and pushing and popping from ...
6
votes
3
answers
221
views
How to reduce the implementation code of lots of wrapper classes?
I am developing a library with some classes, let's call them C1, C2 and ... Cn. Each of these classes realize some interfaces, i.e. I1, I2, ... Im. (n > m). The relationship between objects in the ...
0
votes
0
answers
72
views
weak_ptr function binding? Knowing when a weak_ptr becomes expired?
I would like some help and an explanation please, I am a bit confused :(
I have a 'weak' manager which holds weak_ptr's to other objects in my program. I would like to know when a weak_ptr becomes ...
1
vote
2
answers
108
views
What variables does weak_ptr hold?
I understand about what methods are available and what they are. Please, describe private section of weak_ptr class or give example of some custom weak_ptr code. I can't understand by std::weak_ptr ...
2
votes
2
answers
552
views
About "circular reference", I used weak_ptr but memory leak still happened
I read:
How to avoid memory leak with shared_ptr?
I know that I need to use weak_ptr to avoid circular reference .
So I created a little program to play circular reference.
Following object(...
5
votes
2
answers
4k
views
terminate called after throwing an instance of 'std::bad_weak_ptr' what(): bad_weak_ptr?
I'm learning smart pointers and shared_from_this. In Class Inheritance Relations, it will be very hard to understand.
I have two base classes CA and CB, they are derived from enable_shared_from_this,...
2
votes
1
answer
1k
views
Casting shared_ptr<Type> to weak_ptr<void> and back
How would I get a weak_ptr<void> to a shared_ptr<Type>?
How would I lock a weak_ptr<void> and ultimately produce a shared_ptr<Type>?
Type has a non-trivial destructor, is it ...