Technical Q&A QA1431

How do I use asserts while debugging?

Q:  Why don't my assertion failures log or break when I'm debugging?

A: Why don't my assertion failures log or break when I'm debugging?

The "check", "verify" and "require" macros defined in <AssertMacros.h> generate "production" (i.e. non-debug) code by default. In order for these macros to generate debug code that outputs to stderr and breaks in the debugger you have to either have DEBUG defined to non-zero or DEBUG_ASSERT_PRODUCTION_CODE defined to zero when the <AssertMacros.h> header is included.

To test this create a new Xcode 2.2 project for a standard tool, and replace the contents of the default <main.c> file with the following text:

Listing 1  <main.c>

#include <stdio.h>
#include <AssertMacros.h>

int main( int argc, char* argv[] )
{
    int error = 1;

    verify_noerr( error );
    require_noerr( error, Oops );

    printf("You shouldn't be here!\n");

Oops: ;
    return error;
}

If you build this project for the debug configuration, set a break point on the first line of main and then single step through you'll see that the "verify_noerr" is ignored and the "require_noerr" will (silently) branch to the "CantDoAnything" label. (Note: click the "console" button in the debugging windows toolbar to open the gdb output pane.) This is the expected behavior for production code. Now add "#define DEBUG 1" as the first line before the includes, build, run and single step thru the code again. This time the debugging console should output the following text as you step thru the code:

AssertMacros: error == 0 ,  file: /<your/path/here>/main.c, line: 10 AssertMacros: error == 0 ,  file: /<your/path/here>/main.c, line: 11

To define DEBUG in your project you can do any of the below:



Document Revision History


DateNotes
2006-06-30

New document that how do I enable asserts so they log their messages and/or break when I'm debugging?