Why and when do I need to supply my own deleter?

  • A+
Category:Languages

Why and when do I need to supply my own deleter? Isn't keyword delete sufficient enough?

If you use a smart pointer to manage a resource other than memory allocated by new, remember to pass a deleter.

C++ Primer 5th is taking a pseudo network connection (do not define destructors) to illustrate.

Bad:

struct destination; // represents what we are connecting to struct connection; // information needed to use the connection connection connect(destination*); // open the connection void disconnect(connection); // close the given connection void f(destination &d /* other parameters */) { // get a connection; must remember to close it when done connection c = connect(&d); // use the connection // if we forget to call disconnect before exiting f, there will be no way to closes } 

Good:

void end_connection(connection *p) { disconnect(*p); } void f(destination &d /* other parameters */) { connection c = connect(&d); shared_ptr<connection> p(&c, end_connection); // use the connection // when f exits, even if by an exception, the connection will be properly closed } 

Full context screenshot (I clear some unrelated text):
Why and when do I need to supply my own deleter?

Why and when do I need to supply my own deleter?

 


You need to supply your own delete to the smart pointer creation when the standard delete is not appropriate for deallocating, releasing, discarding, or otherwise disposing of a resource whose life time is being governed by the smart pointer.

A typical use of a smart pointer is to allocate memory as the resource being managed by the smart pointer so that when the smart pointer goes out of scope, the resource being managed, in this case memory, is discarded by using the delete operator.

However in the case of some resource other than memory, in the case of your example a network connection, the use of the delete operator is not appropriate since the resource is not memory that was allocated by using the new operator.

So when using a smart pointer for this kind of resource where the delete operator is not appropriate, you need to supply your own deleter method or function or operator which the smart pointer will use when it goes out of scope and triggers its own destructor which will in turn handle the discarding of any resources that are being managed by the smart pointer.

In the case of your example, the network connection needs to be closed properly and any resources associated with the network connection released in an orderly fashion. While this cleanup of the network connection resources may involve using the delete operator on some allocated memory as part of the cleanup, that action is not the only thing that needs to be done.

There may be other actions such as first flushing the socket and then doing a close on the socket and then deleting any buffers that were allocated, notifying any handlers that may be linked to the connection object that the network connection is being closed so that those handlers can do their own cleanup.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: