By default shared_ptr calls the,

function for clean up when reference count of pointer its pointing to becomes 0.
But its not always that we want to use delete function in destruction, there can be other requirements too like,

1.) If shared_ptr object points to an array instead of a simple pointer,

std::shared_ptr p3(new int[12]);

In its destructor shared_ptr will try to delete the

by calling

function, whereas as correct way is using


In such case we can attach a callback with shared_ptr object that will be called from its destructor for deletion. Check below example,

We can also attach Function objects and Lambda functions as callback i.e.,

2.) There might be other cases when we don’t need to delete any memory we just need to releasing memory or resources to a pool etc.

Check out an example with dummy implementation of Memory Pool and Custom Deletor.

Click Here to Subscribe for more Articles / Tutorials like this.