Detecting and Isolating Memory Leaks
The ability to dynamically allocate and deallocate memory is one of the strongest features of C/C++ programming, but the greatest strength can also be the greatest weakness. This is certainly true of C/C++ applications, where memory-handling problems are among the most common bugs.
One of the most subtle and hard-to-detect bugs is the memory leak — the failure to properly deallocate memory that was previously allocated. A small memory leak that occurs only once may not be noticed, but programs that leak large amounts of memory, or leak progressively, may display symptoms ranging from poor (and gradually decreasing) performance to running out of memory completely. Worse, a leaking program may use up so much memory that it causes another program to fail, leaving the user with no clue to where the problem truly lies. In addition, even harmless memory leaks may be symptomatic of other problems.
Fortunately, the Visual Studio debugger and C run-time (CRT) libraries provide you with effective means for detecting and identifying memory leaks. To understand how to detect memory leaks using the CRT debugging facilities, read the following topics:
- Enabling Memory Leak Detection
- Interpreting Memory Block Types
- Setting a Breakpoint on a Memory Allocation Number
- Comparing Memory States
MFC provides its own set of facilities for detecting memory leaks in MFC programs. For more information, see Detecting Memory Leaks in MFC.