Open In App

weak_ptr in C++

Last Updated : 08 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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_ptr

To 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_ptr

The 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 Functions

The following are some member functions associated with std::weak_ptr to provide different functionalities.

       S.No.                     Functions             Description

1

reset() Clear the weak_ptr.

2

swap Specialization of std:swap(). It swaps the objects managed by weak_ptr.

3

expired()      Check if the resource weak_ptr pointing to exists or not.

4

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.

5

use_count() Tells about how many shared_ptr owns the resource.

Example of weak_ptr

The 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;
}


Output

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_ptr

The following are the primary applications of the weak_ptr:

  • Preventing Circular References: The primary application of weak_ptr is to prevent circular references. When an object wants to reference another object without owning it, it can use weak_ptr. This ensures that no circular references are created and objects can be safely freed when they are no longer needed.
  • Cache Systems: weak_ptr is commonly used in cache implementations. Caches often need to temporarily store references to objects without preventing the deletion of those objects when they are no longer in use. weak_ptr provides an elegant solution for this use case.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads