mod_annot editor

Annotate Section

The Problem of Resource Management

The basic problem of resource management is of course well-known to programmers. When you allocate a resource, you should ensure it is released again when you've finished with it. For example:


	char* buf = malloc(n) ;
	... check buf is non null ...
	... do something with buf ...
	free(buf) ;

or

	FILE* f = fopen(path, "r") ;
	... check f is non null ...
	... read from f ....
	fclose(f) ;

Clearly, failure to free buf or to close f is a bug, and in the context of a long-lasting program such as Apache it would have serious consequences up to and including bringing the entire system down. So we need to get it right!

In trivial cases as presented above, this is straightforward. But in a more complex case with multiple error paths, and even the scope of a resource being uncertain at the time it is allocated, it becomes a serious problem to ensure cleanup takes place in every execution path. So we need a better way to manage resources.