Has this ever happened to you? You allocate some sort of resource which needs to be released before returning from the function. You can put the release at the end of the routine for the normal case, but there are a number of error checks which can cause it to return early. You consider calling the reclaim routine inside each error handler, but you're concerned that someone maintaining the code in the future will forget to do so. Instead you put all of the cleanup code at the end of the function, and have each error check use a goto.
Then of course someone argues very strenuously that goto is evil incarnate and must never be used, one thing leads to another, and then you have to find somewhere to hide the body ... wait, nevermind that last bit.
Consider this instead:
int foo() { int fd LOCAL_SCOPE_FD = open("/path/to/file"); if (error1()) { return -1; } return 0; }
Though it might appear that I'm suggesting the file descriptor be leaked in order to simplify the code, that is not the case. The magic happens in LOCAL_SCOPE_FD:
#define LOCAL_SCOPE_FD __attribute__((cleanup(local_fd_close))) void local_fd_close(int *fd) { if (*fd >= 0) close(*fd); }
__attribute__(cleanup) is a gcc extension. When an automatic variable goes out of scope, the function indicated by the cleanup attribute will be called. If the scope is exited unusually, such as via longjmp() or by calling exit(), the cleanup function does not get called, but normal return statements or falling off the end of the block do work.
It also works within inner blocks. For example:
int foo() { if (do_something) { int fd LOCAL_SCOPE_FD = open("/path/to/file"); } /* local_fd_close will be called here. */ ... more code ... }
Cleanup can only be applied to automatic variables, i.e. variables on the stack declared within a function. It cannot be used with global or static variables. The cleanup attribute can be used with any variable type, not just integers. The cleanup function receives a pointer to the automatic variable being cleaned up.
I assume that if the Gentle Reader is reading this article, there are good reasons to use C in your problem space. Needless to say, if you want automatic resource reclamation a language other than C would provide more capabilities. Garbage collection and object finalizers are powerful constructs, given a problem space where they are appropriate.
Acknowledgements
Many thanks to Matt Peters for pointing out the __attribute__(cleanup) capability to me.
Ian Lance Taylor recently wrote on a similar topic, about support for destructors and exceptions in C.
Updates
This article was picked up on reddit, with a few comments. One pertinent comment from erikd:
His local-fd-close() function has a bug, it needs to check the return value of close(), because close can return an error of EINTR which means the prcoess received an interrupt and should retry the close operation.