Visual Studio doesn't break on assert violation

Viewed 1010

I'm debugging a CMake target in Visual Studio 2019 for a project which is managed by CMake and built using MinGW.

However, when an assertion fails the debugger simply quits without giving me a call stack or letting me inspect the current state of the program. (Normal breakpoints do suspend execution as expected.)

breakpoints_work

I've enabled Break When Thrown for all C++ Exceptions in Debug > Windows > Exception Settings, but to no avail.

How can I make Visual Studio break execution when an assertion fails?

2 Answers

If your project is a "Console" application rather than a "Windows" application and you have used <assert.h> then according to the documentation:

The destination of the diagnostic message depends on the type of application that called the routine. Console applications receive the message through stderr. In a Windows-based application, assert calls the Windows MessageBox function to create a message box to display the message with three buttons: Abort, Retry, and Ignore

So a possible solution is to temporarily build your program as a "Windows" app by adding the WIN32 option to your executable definition:

add_executable(<name> WIN32 [source1] [source2 ...])

It is possible that the NDEBUG macro is enabled in your project and that stops VS from breaking on assertion failure.

From cppreference

If NDEBUG is defined as a macro name at the point in the source code where is included, then assert does nothing.

If NDEBUG is not defined, then assert checks if its argument (which must have scalar type) compares equal to zero. If it does, assert outputs implementation-specific diagnostic information on the standard error output and calls std::abort. The diagnostic information is required to include the text of expression, as well as the values of the standard macros FILE, LINE, and the standard variable func (since C++11).

Related