Chapter 4 Diagnostic Services

This chapter describes a group of macros and global functions that provide diagnostic services. All these functions, except as noted, require the Debug version of the Microsoft Foundation Class Library.

In the Debug library, all allocated memory blocks are bracketed with a series of “guard bytes.” If these bytes are disturbed by an errant memory write, then the diagnostic routines can report the problem.

If you include the line

#define new DEBUG_NEW

in your implementation file, then all calls to new will store the filename and line number where the memory allocation took place. The DumpAllObjectsSince function of the CMemoryState class will display this extra information, thus greatly simplifying the identification of memory leaks.

Since many of these diagnostic functions are designed for tracking memory errors, you should refer to the “Memory Management” section in Chapter 7 of the Class Libraries User's Guide for a discussion of memory allocation for both MS-DOS and Windows while using the Microsoft Foundation Class Library. Refer also to the class CDumpContext for additional information on diagnostic output. The cookbook section of the Class Libraries User's Guide, “Detecting Memory Leaks” (page 290), illustrates the use of several key memory-diagnostic functions.

For a general discussion of diagnostic facilities, see Chapter 11, “Diagnostics,” in the Class Libraries User's Guide.

To use these macros and global functions, add the following directives to the top of your program:

#define _DEBUG

#include <afx.h>