Empty release of ASSERT macro crashes?

Take a look at this code:

#include <cassert> #ifdef DEBUG #define ASSERT(expr) assert(expr) #else #define ASSERT(expr) #endif /* DEBUG */ 

The program will only work if I define DEBUG , otherwise it will freeze and end without any results. I am using MinGW in an Eclipse Indigo CDT. Advice appreciated!

+6
source share
2 answers

You almost certainly abuse allegations . An assertion expression should never have side effects.

When you say assert(initialize_critical_space_technology()); and then omit the whole line in the release build, you can imagine what will happen.

The only safe and reasonable way to use statements is with values:

 const bool space_init = initialize_critical_space_technology(); assert(space_init); 

Some people introduce the VERIFY macro to always execute code:

 #define VERIFY(x) (x) // release #define VERIFY(x) (assert(x)) // debug 
+7
source

It's hard to say without looking at the actual code causing the problem. My guess: you evaluate an expression with side effects in ASSERT() . For example, ASSERT( ++i < someotherthing ) in the loop. You can confirm by temporarily changing the macro definition only expr in NDEBUG lines. After confirming this reason, go to each ASSERT call that you issue to ensure that the expressions are free from side effects.

+8
source

Source: https://habr.com/ru/post/901650/


All Articles