How Can I View the Call Stack After an MFC Assert?

Full Problem Description: When debugging an application that uses the MFC library, an ASSERT gives the file and line number of its location, but I want to know how the code got to the assertion. How can I do this?

You need to examine the call stack. An ASSERT gives you three choices: Abort, Retry, or Ignore. Abort and Ignore do not activate the debugger, so there is no way to examine the call stack. If you choose Retry, the breakpoint (interrupt 3) embedded in your source code will cause a break and activate the debugger. The debugger will also be activated if you have enabled Just-in-time debugging. To examine the call stack, on the View menu, click Debug Windows and Call Stack.

Another way to catch an MFC assertion is by setting a breakpoint on the opening brace of the AfxAssertFailedLine function in AFXASERT.CPP. This source file is found in the MFC\SRC subdirectory of your Visual C++ installation directory.

The AfxAssertFailedLine function is called if an ASSERT fails. By setting a breakpoint on the opening brace of this function, the debugger will automatically come up when it finds an ASSERT in your code, or in the MFC library code. At this point, you can examine the call stack.

Remember that you cannot use the call stack to trace back through Windows messages.