Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off ASSERTs in debug mode in Visual Studio 2013

Tags:

c++

assert

mfc

Is there any way to turn off asserts instead of switching to Release mode. I need to debug a code which make assertions really often and it slows down my work. These asserts are not related to the issue i am trying to solve, so for now they only slow down my progress, because they are called very often in one of my base classes. Now I don't have the time to improve their design, so can someone tell me if there is a way to turn off asserts while being in debug mode and using it's capabilities.

like image 655
Alexander Demerdzhiev Avatar asked Jan 20 '15 09:01

Alexander Demerdzhiev


People also ask

Does assert only work in debug mode?

By default, the Debug. Assert method works only in debug builds. Use the Trace. Assert method if you want to do assertions in release builds.

How do I turn off assert in C++?

We can disable assertions in a program by using NDEBUG macro. Using NDEBUG macro in a program disables all calls to assert.

How do I disable debug mode in Visual Studio code?

To enable or disable Just My Code in Visual Studio, under Tools > Options (or Debug > Options) > Debugging > General, select or deselect Enable Just My Code.

How do asserts help with debugging?

An Example of Debugging With Assertions You'll typically use assertions to debug your code during development. The idea is to make sure that specific conditions are and remain true. If an asserted condition becomes false, then you immediately know that you have a bug.


2 Answers

You can use _set_error_mode or _CrtSetReportMode (see xMRi's answer) to alter failure reporting method and avoid modal dialog box. See code snippet there:

int main()
{
   _set_error_mode(_OUT_TO_STDERR);
   assert(2+2==5);
}

Also note that assert failures are typically for a reason, and you want to fix code, not just suppress the report. By removing them from debug builds completely you are simply breaking good things built for you.

like image 119
Roman R. Avatar answered Oct 13 '22 22:10

Roman R.


User _CrtSetReportMode

int iPrev = _CrtSetReportMode(_CRT_ASSERT,0);
// Start Operation with no ASSERTs
...
// Restore previous mode with ASSERTs
_CrtSetReportMode(_CRT_ASSERT,iPrev);

Instead of using 0, you can use _CRTDBG_MODE_DEBUG only.

like image 31
xMRi Avatar answered Oct 13 '22 23:10

xMRi