Wednesday, 27 July 2011

MFC ASSERT trap

ASSERT page in MSDN says that "In the Release version of MFC, ASSERT does not evaluate the expression and thus will not interrupt the program. If the expression must be evaluated regardless of environment, use the VERIFY macro in place of ASSERT". This can be somewhat misleading as ASSERT will definitely not bring up Assert Dialog Box in Release build of your program but can break its logic if expression within ASSERT macro changes program's state. Line with ASSERT is removed in Release build and expression is never executed!

This kind of logical errors can be avoided by using macro VERIFY: it works as ASSERT in Debug mode and expression in its argument is executed in Release mode.


This line is not executed in Release build:

This is safer as foo() will be executed:

VERIFY macro is safe and compact - foo() is always executed:
Post a Comment