Assertions


Click here to change the theme.

For beginner Visual C++ programmers, an assertion error can be an unpleasant surprise and the response to them is usually not clear. An assertion error is usually the result of an MFC ASSERT macro. See the documentation for (ASSERT) for details about it . MFC uses ASSERT macros extensively. If a problem exists in your program that MFC uses an ASSERT macro to detect then the ASSERT macro in a debug version of your program will result in a message box with "Abort", "Retry" and "Ignore" buttons. If you click "Retry" then the debugger will show you were the error occurred. The source code you see will probably be a line of MFC source code but you can easily get the debugger to show you your source code and the line of your program that the problem was encountered at.

The purpose of most ASSERT macros in MFC code is to test for conditions that might be a problem, or will be a problem, if the program were allowed to continue. If you attempt to ignore and/or bypass the assertions then more serious errors are likely to occur. The appropriate response is to look at the MFC source code line containing the ASSERT macro to see what the condition is that MFC is concerned about. Often there are also comments indicating the problem and/or solution. To find the MFC source code line containing the ASSERT macro, debug the program, if you have not already. Then when the assertion failure occurs, choose to debug the program (click "Retry"). The debugger should show the MFC source code with the ASSERT macro.

You will probably need to know where in your own source code that the program is. You can use the call stack to find it. See: Debugging using the Call Stack.

Getting help

It seems that 90% of the time that people post a question in a newsgroup or discussion forum asking for help with an "assertion failure" or something like that they just say that the program is getting an "assertion failure" or something like that. Unfortunately it is not likely that anyone can be of much assistance with such minimal information. If you have looked at the MFC source code that is doing the assert and need help, then please be sure to describe the MFC source code that is doing the assert, and include relevant comments in the MFC source code. Also include the line of you source that is getting the assertion error and relevant previous lines.