Using `std::shared_ptr` with custom deleter functions

In C++, the std::shared_ptr class from the C++ Standard Library is a powerful smart pointer that provides shared ownership of dynamically allocated objects. By default, std::shared_ptr uses delete as the default deleter function to automatically free the memory when the last std::shared_ptr referencing the object is destroyed.

However, there are scenarios where you might want to use a custom deleter function instead of the default delete operator. For example, when working with resources other than dynamically allocated memory, such as file handles or network connections, you may need to release these resources in a specific way.

Fortunately, std::shared_ptr provides a way to specify a custom deleter function during its initialization. The deleter function is responsible for releasing the resource held by the std::shared_ptr when it goes out of scope.

Here’s an example of using std::shared_ptr with a custom deleter function:

void customDeleterFunction(ResourceType* resource) {
    // Custom logic to release the resource
    // e.g., closing a file, terminating a network connection, etc.
}

int main() {
    // Create a shared_ptr with a custom deleter function
    std::shared_ptr<ResourceType> sharedResource(
        new ResourceType(),  // Pointer to the resource
        customDeleterFunction // Custom deleter function
    );

    // Access the resource through the shared_ptr
    sharedResource->doSomething();
    
    // No need to manually release the resource
    // shared_ptr automatically calls the custom deleter function

    return 0;
}

In the above example, ResourceType is a user-defined type representing a resource. The customDeleterFunction is a user-defined function that takes a pointer to the resource and implements the logic to release the resource.

By passing the customDeleterFunction to the std::shared_ptr constructor as the second argument, we associate the custom deleter function with the shared_ptr. When the last shared_ptr referencing the resource goes out of scope, the custom deleter function will be automatically called to release the resource.

By using std::shared_ptr with custom deleter functions, you can ensure that resources are correctly released even in complex scenarios where multiple shared_ptr instances are involved. This allows for safer and more robust resource management in C++ programs.

#cplusplus #smartpointers