Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable the debug assertion dialog on Windows?

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.

like image 927
Matt Fichman Avatar asked Dec 19 '12 00:12

Matt Fichman


People also ask

How do I disable assert in Visual Studio?

In Visual Studio, go to Debug / Windows / Exception Settings. In the Exception Settings, go to Win32 Exceptions / 0xc0000420 Assertion Failed. Uncheck the box in front of that entry.

What is a debug assertion?

Assert(Boolean, Debug+AssertInterpolatedStringHandler) Checks for a condition; if the condition is false , outputs a specified message and displays a message box that shows the call stack. Assert(Boolean, String)

What does debug assertion failed mean?

An assertion statement specifies a condition that you expect to hold true at some particular point in your program. If that condition does not hold true, the assertion fails, execution of your program is interrupted, and this dialog box appears. Click. To. Retry.


2 Answers

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?

like image 97
HerrJoebob Avatar answered Sep 21 '22 16:09

HerrJoebob


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.

like image 33
KindDragon Avatar answered Sep 24 '22 16:09

KindDragon