21

I have a bunch of unit tests that I'm running in batch mode. Occasionally, one will crash with a debug assertion fired from the Visual C++ library. This causes a dialog to pop up, and the unit tests stop running until I click "OK" to close the dialog.

How can I make a C++ program just crash (like on Linux) when it hits an assertion, instead of popping up the annoying dialog?

Note: I do not want to disable assertions; just the dialog.

Matt Fichman
  • 5,458
  • 4
  • 39
  • 59

3 Answers3

19

Check out _CrtSetReportHook():

http://msdn.microsoft.com/en-us/library/0yysf5e6.aspx

MSDN advertises this as a robust way for an application to handle CRT runtime failures like assertions. Presumably you can define a report hook that dumps your process:

How to create minidump for my process when it crashes?

Community
  • 1
  • 1
HerrJoebob
  • 2,264
  • 16
  • 25
17

This code will disable display of dialog. Instead, it will print an error in the output window, and stderr.

int main( int argc, char **argv )
{
     if( !IsDebuggerPresent() )
     {
          _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG );
          _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDERR );
     }

     ...
}

The same must be applied for _CRT_ERROR if you use Q_ASSERT from Qt library.

Alexander Dyagilev
  • 1,139
  • 1
  • 15
  • 43
KindDragon
  • 6,558
  • 4
  • 47
  • 75
0

I put the code for a suitable hook (that you can install with _CrtSetReportHook()) in: https://stackoverflow.com/a/28852798/2345997

Community
  • 1
  • 1
user1976
  • 353
  • 4
  • 15