MFC Debugging Support

The Microsoft Foundation Class Library (MFC) and Visual C++ help you debug your applications in a variety of ways. This topic presents a few useful general debugging techniques, followed by more detailed debugging topics.

The Microsoft Foundation Class Library (MFC) contains many diagnostic features to help debug your program during development, such as the Dump and AssertValid member functions, the TRACE and ASSERT macros, the AfxEnableMemoryTracking and AfxDebugBreak global functions, and the CMemoryState class to detect memory leaks.

Because diagnostics slow down and interrupt your application, they are not suitable for a release version. When you are developing your program, you typically build a Win32 Debug version of your program and link with the debug version of MFC. Once the program is completed and debugged, you build a Win32 Release version and link with the release version of MFC. You can easily switch between Win32 Debug and Win32 Release by setting the Select Active Configuration droplist.

Following are several techniques for debugging your MFC application:

  • Before you start a debugging session (or as soon as possible after you start), arrange the debugger and the program you are debugging on the screen so that neither overlaps the other. Otherwise, there may be situations in which the debugger completely obscures the program being debugged.

  • Turn on Multiple application debugging from the TRACER application when you are debugging an application and one or more DLLs. The Multiple application debugging option prefixes each trace statement with the name of the application that generated it. TRACER can be found in the BIN directory, and an icon for it is installed in the Microsoft Visual C++ program group with the name “MFC Tracer.”

  • If you have trouble setting breakpoints with the debugger, you can hard-code them into your application with the following statement:

    DebugBreak( );
    

    which is platform-independent. For MFC applications, you can also use:

    AfxDebugBreak( );
    

    This does an

    _asm int 3
    

    in Intel versions and calls DebugBreak on other platforms. The advantage (on Intel) is that you break in source code rather than in kernel code.

    Be sure to remove these statements when building release-mode applications or include them under #ifdef _DEBUG.

  • If you run into limitations with the Visual C++ debugger, you can always use TRACE statements and the TRACER application. Activating the Multiple application debugging option in TRACER can be very useful in tracking the order of events.

For additional information specific to MFC debugging, see the following topics: