Last Updated : 08 Jan, 2024
The weak_ptr is one of the smart pointers that provide the capability of a pointer with some reduced risks as compared to the raw pointer. The weak_ptr, just like shared_ptr has the capability to point to the resource owned by another shared_ptr but without owning it. In other words, they are able to create a non-owning reference to the object managed by shared_ptr.
Need of weak_ptrTo understand the need for weak_ptr, we need to first understand the use case of shared_ptr which leads to a common problem called a circular link. It occurs when two or more objects reference each other using a shared_ptr. For example, if ObjectA has a shared_ptr to ObjectB and ObjectB has a shared_ptr to ObjectA, they form a circular reference. This can be problematic because neither ObjectA nor ObjectB will ever be deleted, leading to a memory leak.
Here, weak_ptr comes to the rescue by providing a way to break these circular references. It allows you to create a non-own reference to an object managed by shared_ptr without affecting the reference count or preventing the object from being deleted.
Syntax of weak_ptrThe weak_ptr can be declared using the following syntax:
std::weak_ptr<type> name;
where type is the type of data it is pointing to.
std::weak_ptr Member FunctionsThe following are some member functions associated with std::weak_ptr to provide different functionalities.
reset() Clear the weak_ptr. swap Specialization of std:swap(). It swaps the objects managed by weak_ptr. expired() Check if the resource weak_ptr pointing to exists or not. lock() If the resource pointed by weak_ptr exists, this function returns a shared_ptr with ownership of that resource. If the resource does not exist, it returns default constructed shared_ptr. use_count() Tells about how many shared_ptr owns the resource. Example of weak_ptrThe following example demonstrates how the weak_ptr solves the circular reference problem.
C++
// C++ program to illustrate the use of weak_ptr
#include <iostream>
#include <memory>
using namespace std;
// declaring a dummy object
class Object {
public:
Object(int value)
: data(value)
{
cout << "Object created with value: " << data
<< endl;
}
~Object()
{
cout << "Object destroyed with value: " << data
<< endl;
}
int data;
};
// driver code
int main()
{
// creating shared pointer with resource ownership
shared_ptr<Object> sharedObjectA
= make_shared<Object>(42);
// creating weak pointer to the previously created
// shared objects
weak_ptr<Object> weakObjectA = sharedObjectA;
// Access objects using weak_ptr
if (!weakObjectA.expired()) {
cout << "The value stored in sharedObjectA:"
<< (*weakObjectA.lock()).data << endl;
}
// deleting object
sharedObjectA.reset();
cout << "End of the Program";
return 0;
}
Object created with value: 42 The value stored in sharedObjectA:42 Object destroyed with value: 42 End of the Program
In this example, we create a shared_ptr with the ownership of a dynamically created object. Then we created a weak_ptr which refers to the same resource as that of shared_ptr. After resetting the shared_ptr, we can see that its is destroyed and deallocated from the memory.
Applications of weak_ptrThe following are the primary applications of the weak_ptr:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4