I need to only run ship build and I need to assert on certain condition in release build to see if the problem is fixed. How do I do it?
Undefine the NDEBUG macro - you can do this locally around the asserts you want to remain in the build:
#undef NDEBUG
#include <assert.h> // reinclude the header to update the definition of assert()
or do whatever you need to do so your build process does not define the NDEBUG macro in the first place.
The default behaviour of ASSERT is to abort the program under the Debug configuration, but this generally becomes a no-op under a Release configuration. I believe it does this by checking for the existence of the preprocessor NDEBUG macro. I'm not at work at the moment so cannot check this.
I think the easiest way around this is to modify the Debug configuration to turn all optimisations up to the same level as Release (O2 from memory), and then re-build your software. This will give you the equivalent performance and speed of a Release build, but it will still define the NDEBUG preprocessor macro which means all failed ASSERTs will still cause the program to abort. Just remember to change the optimisation level back later, otherwise you will have trouble debugging under the Debug configuration.
In general though, ASSERTs should only be used for programming preconditions and never to handle failures in shipping software. You want to fail quickly during development, but gracefully in front of a user.
Why not just define your own assert:
#define assert(x) MessageBox(...);
Just call directly the part of the assert
macro definition that is active in release mode.
You can find very useful definitions for assertions in C++ in this great article by Miro Samek. Then you can tweak them a bit to satisfy your needs. For example, you might create another macro, release_assert
, that does the same as assert but regardless of whether it's in release or debug mode.
I like to define it to throw some sort of assert_exception derived from a std::runtime_error. Then catch it somewhere and do something useful.
Actually - I'd go with shipping the debug version if you can live with it. If you don't need the performance of the release version, use the debug. It tend's to have fewer bugs (this is a gross oversimplification and if you program is free of bugs, just switching to release does not change this, but due to things the compiler does in debug mode, the bugs may not occur and/or have less severe consequences).
Perhaps it is also possible to optimize only the time critical parts of you program.
It can also simplify debugging.