Weak Pointers with std::weak_ptr

Using Custom Deleters with Weak Pointers

Can I use custom deleters with weak pointers like I can with unique_ptr and shared_ptr?

Illustration representing computer hardware

No, you cannot directly specify custom deleters for weak pointers like you can with unique_ptr and shared_ptr. Weak pointers do not participate in the ownership or destruction of the pointed-to objects. Instead, they rely on the corresponding shared pointers to manage the object's lifetime and deletion.

When you create a shared pointer with a custom deleter, the deleter is stored in the control block associated with the shared pointer. The control block is shared among all the shared pointers and weak pointers that refer to the same object. When the last shared pointer to the object is destroyed, the custom deleter is invoked to properly cleanup the resource.

Here's an example that demonstrates using a custom deleter with shared_ptr:

#include <memory>
#include <iostream>

class MyClass {
public:
  ~MyClass() {
    std::cout << "Destructor called\n";
  }
};

struct CustomDeleter {
  void operator()(MyClass* ptr) {
    std::cout << "Custom deleter called\n";
    delete ptr;
  }
};

int main() {
  std::shared_ptr<MyClass> SharedPtr{
    new MyClass{}, CustomDeleter{}
  };

  std::weak_ptr<MyClass> WeakPtr{SharedPtr};
}
Custom deleter called
Destructor called

In this example, we create a shared pointer SharedPtr to a dynamically allocated MyClass object, and we specify a CustomDeleter as the second argument to the shared_ptr constructor. The CustomDeleter is a functor that overloads the function call operator to define how the object should be deleted.

We then create a weak pointer WeakPtr from the SharedPtr. The weak pointer itself does not store or manage the custom deleter. It simply refers to the object managed by the shared pointer.

When the SharedPtr goes out of scope and is destroyed, it checks if it is the last shared pointer referencing the object. Since it is, it invokes the CustomDeleter to cleanup the resource, and then the destructor of MyClass is called.

Although weak pointers do not directly support custom deleters, they can still be used in conjunction with shared pointers that have custom deleters. The custom deleter will be invoked when the last shared pointer is destroyed, regardless of any weak pointers that may still reference the object.

If you need to perform custom cleanup logic when the object is destroyed, you should define the custom deleter in the shared pointer that owns the object, and weak pointers can be used to observe the object without affecting its lifetime.

This Question is from the Lesson:

Weak Pointers with std::weak_ptr

A full guide to weak pointers, using std::weak_ptr. Learn what they’re for, and how we can use them with practical examples

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Weak Pointers with std::weak_ptr

A full guide to weak pointers, using std::weak_ptr. Learn what they’re for, and how we can use them with practical examples

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved