Sign in to follow this  
derek7

assert question

Recommended Posts

It could do anything from nothing to making your computer burst into flames.... [grin]

More seriously though, it *can* do nothing int release builds (it's an empty #define in that case), or in debug builds it will usually display a message saying that the assert has failed and then terminate the program (sometimes you'll get the option to use your debugger to break where the assert failed).

Share this post


Link to post
Share on other sites
Usually it'll pop up an error dialog. However, there's nothing stopping you from writing your own assert handler; it's easy.

At its simplest, you could use something like this:


#define assert(x) if(!(x)) __asm int 3;


That "int 3" will cause the debugger to pause your program as if it had hit a breakpoint so you can investigate exactly why the assertion has failed. (I use a define instead of a function so that it breaks exactly on the assertion, instead of you having to walk back up the callstack).

A more complex form could be a define and a function working together:


#define assert(x) if(ShouldBreak(x, #x, __FILE__, __LINE__)) __asm int 3;

bool ShouldBreak(bool success, char* expr, char* filename, int lineno)
{
if(success) return false;
char buf[1024]; snprintf(buf, "An assertion failed in %s line %i:\n\n%s\n\nPress OK to continue or Cancel to debug.", 1024, filename, lineno, expr);
return (IDCANCEL == MessageBox(buf, "Assertion failed", MB_OK_CANCEL));
}


That would let you ignore the assertion and continue on if you wanted. You could also do things like log the assertion to a file, report it to a server, add one to a counter of the number of times it has been hit, and so on.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this