Dangers of manual resource management
C++ allows us to manage resources almost at the hardware level, and someone, somewhere, must indeed manage them at this level. The latter is actually true for every language, even the high-level ones that do not expose such details to the programmers. But somewhere does not have to be in your program! Before we learn the C++ solutions and tools for resource management, let’s first understand the problems that arise from not using any such tools.
Manual resource management is error-prone
The first and most obvious danger of managing every resource manually, with explicit calls to acquire and release each one, is that it is easy to forget the latter. For example, see the following:
{ object_counter* p = new object_counter; ... many more lines of code ... // Were we supposed to do something here? // Can't remember now... }
We are now leaking a resource (the object_counter
objects...