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

Calling a function on weak pointer

I have written the following code: #include #include using namespace std; class Foo { int x = 0; public: int get() const { return x; } void set(int x) { this->x = x; } }; int main(){ auto sp =…
sunny
  • 149
  • 8
0
votes
1 answer

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. It cause sigtrap because the destruction of…
xubury
  • 84
  • 6
0
votes
1 answer

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 need to access the member functions of class X…
Harini Sj
  • 173
  • 1
  • 2
  • 11
0
votes
0 answers

what is the lifetime of a shared pointer inside a constructor of an instance?

my first question: If I create a shared_ptr inside of class B constructor: class B{ public: B(){ auto a = make_shared(); } }; will the owner of the object A be the instance of B or only the constructor of B? in other…
migara
  • 55
  • 1
  • 5
0
votes
0 answers

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 WeakPtr::WeakPtr(const SharedPtr&…
ADV
  • 179
  • 9
0
votes
1 answer

I can't understand weak pointers (C++)

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 really help either. Thank you in advance!
H-005
  • 467
  • 3
  • 15
0
votes
0 answers

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 use that won't keep them alive longer than necessary,…
imMAW
  • 173
  • 1
  • 1
  • 6
0
votes
0 answers

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 thus owned by container itself) and some by…
yesint
  • 145
  • 9
0
votes
0 answers

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 expired at the point it becomes expired (i.e the…
lfgtm
  • 1,037
  • 6
  • 19
0
votes
0 answers

creating weak_pointer from a shared_pointer and reusing a basic pointer

Why is wp5 not pointing to an Integer whose value is 50? #include #include #include "DoubleLinkedList.h" class Integer { private: int number; public: Integer(int number) : number(number) {} int get() { return…
Antonio Santoro
  • 827
  • 1
  • 11
  • 29
0
votes
1 answer

Empty weak pointer in enable_shared_from_this

After publicly inheriting enable_shared_from_this and initialzing the object of class, while calling another function of that class, i can still see empty weak pointer of enable_shared_from_this_class while debugging in Visual Studio. All existing…
0
votes
0 answers

C++ boost::asio connection weak_ptr err

I have a problem in C++ TCP Server(using boost asio). In normal client, Many clients connect to server is normal. But if every client connect and disconnect server 10 times/sec, Server core some time. (1)create server like this: void…
zzhongcy
  • 17
  • 4
0
votes
2 answers

Locating a weak_ptr after shared_ptr is expired

I have a struct, A, objects of which are managed by shared_ptrs. Struct A holds a reference to struct B. B objects need to keep track of which A objects hold references to them, and also need to be able to return shared_ptrs to these objects. To…
Kevin
  • 510
  • 4
  • 16
0
votes
3 answers

Can I check whether `shared_from_this` is safe to call?

When calling shared_from_this from within types that inherit from enable_shared_from_this, very bad things (TM) can happen, if this is not currently held by a shared_ptr object (typically, the application crashing). Is it possible in C++14 (not 17)…
bitmask
  • 32,434
  • 14
  • 99
  • 159
0
votes
0 answers

Iterate over multimap>

I am trying to iterate over a multimap> but for some reason using the for loop below causes an error. using RenderIndex = int32_t; class RenderObject { public: RenderIndex m_render_index; // rest of class…
Matthew
  • 800
  • 3
  • 12
  • 35