Debug mode or Release mode - c++

Recently, I'm working on intergrating the physics engine into my graphics engine program. Before this, I always build my program in Debug mode because I feel Debug means safe and more information to let me know where is wrong.
In my program, I built Assimp in Release mode, but I still used it in the Debug mode until now. For now, I build Bullet Physics in Release mode beacuse of the performance is huge different in Debug mode. If you want to know how slow it is, you could see this.
The important thing is that I could not use this *.lib file in Debug mode ever, so I have a question, when or why do you change to Release mode from Debug mode or on the other hand. Or using the Release library in Debug mode either? For now, I think I need to change to Release mode permanently for Bullet Physics, and don't know if this is good or bad.
Edit:
I know the benifit for release and debug mode, because there is a lot of possible duplicated articles in stackoverflow, but what I want to know is when you make a program what the decision for choosing or just because encounter a performace problem so need to rebuild the program into release mode like me. I just want to clarify a little different between the posiible duplicated articles :)
There are four cases for this question.
Case 1. Start a new project and programming in Debug mode, because of... .
Case 2. Start a new project and programming in Release mode, because of... .
Case 3. Work in Debug mode a period of time, but change to Release mode because of ... .
Case 4. Work in Release mode a period of time, but change to Debug mode because of ... .
I really want to know what the decisions in the four cases. I'm in the case 3, because of the performace of the Bullet Physics, I decide to make program to be release mode forever, and also make other debug library to rebuild to release mode. In case 3, the question is, I just use the third party library, so do I need to use Debug mode for any reason? Or I just go to release mode?
For case 1&2, Is there any decision why you let the debug mode or release mode to build your new program.
For case 4, I could not think why make a man to change the Release mode to Debug mode, but maybe there is a actual cases? except just build the library you could make this decision.
If my question is not clear enough, please let me know.

"Debug mode" and "Release mode" are two predefined set of compiler switches. You can however override them, and create intermediate forms. C++ commonly benefits a lot from inlining, so a debug-with-inlining build is a reasonable intermediate form. Another common variation is to use the Release version of the C & C++ libraries even in debug builds.
You can even override switches on individual file level. Your bullet physics are likely correct, so you might want to check if they can be compiled with
That said, it's quite common to flip between debug and release mode, when tackling different problems.

Use debug (unoptimized) builds when you need to debug something. They produce (slow) code that matches your source closely so you can more easily debug.
Use release (optimized) builds for code that you ship. They produce code that run a lot faster (orders of magnitude often), but debugging optimized code is really difficult since the generated code is often quite different from the source you wrote.
Mixing objects built as debug/release in the same program is often a recipie for disaster (crashes) (especially with Microsofts compiler); so just don't do that.

Related

How to specify one class of my code not to generate debugging information in debug mode in Visual studio?

Now I'm doing a debug on a piece of code that calls a class that has made sure it's okay, but it's running very slowly in debug mode and much faster in release mode with Visual Studio.
Can I ask this class not to generate debugging information? So in debug mode, this class can also run at a good speed?
What is slowing down your code is not the generation of debug information, but the lack of optimizations. Release mode enables optimizations that allow your compiler to change your code in a way that produces the same result but in a much faster way. See https://en.cppreference.com/w/cpp/language/as_if. Debug mode disallows these optimizations in order to make it easier to debug (it's in the name), at the cost of execution speed. Debug mode can't get rid of any useless code or make big changes. Use Debug mode to make sure everything works properly, and Release to run it fast when it does work.
If you post your code here we can see if there are any glaring problems with it, but without code the best I can do is the general explanation.

Mixing debug and release in MSVC?

I got a program that has a quite simple 'load up' function that takes about 30 seconds (3M triangles that goes into std containers).
It works perfectly well.
The rest doesn't always (it is not finished) so I debug a lot I make a lot of changes and so on which means restarting quite often.
Is there any secret technique to compile the loader in release (which speeds up everything enormously) and leaving the rest as debug?
ps. I use MSVC 2005
Debug builds tend to be very slow on Visual C++. There are a few reasons for this:
the most obvious is that the code is not optimized
the memory allocation functions in the debug CRT library perform additional checks to detect heap corruption and other issues, so they are slower.
many STL functions perform additional validations and assertions on debug builds, which make use of STL containers very slow
I've had success debugging apps that make heavy use of memory and STL using the following method:
use the release build for debugging (yes, this works fine!).
configure your release builds to include debugging symbols, this should make the compiler write the .pdb file that the debugger uses
only for the files you intend to debug, set the compiler to no optimizations.
Note that the above works great to debug problems in your own logic, but it may not be the best idea if you are debugging memory corruption or other problems, since you are eliminating all the extra debug code that the CRT provides for these types of issues.
I hope this helps!
Mixing debug and release builds tends to go horribly wrong.
But there's no reason why you shouldn't turn on optimisation for some selected source files even in a debug build - and the optimisation should give you the performance improvements.

Best practices and tools for debugging differences between Debug and Release builds?

I've seen posts talk about what might cause differences between Debug and Release builds, but I don't think anybody has addressed from a development standpoint what is the most efficient way to solve the problem.
The first thing I do when a bug appears in the Release build but not in Debug is I run my program through valgrind in hopes of a better analysis. If that reveals nothing, -- and this has happened to me before -- then I try various inputs in hopes of getting the bug to surface also in the Debug build. If that fails, then I would try to track changes to find the most recent version for which the two builds diverge in behavior. And finally I guess I would resort to print statements.
Are there any best software engineering practices for efficiently debugging when the Debug and Release builds differ? Also, what tools are there that operate at a more fundamental level than valgrind to help debug these cases?
EDIT: I notice a lot of responses suggesting some general good practices such as unit testing and regression testing, which I agree are great for finding any bug. However, is there something specifically tailored to this Release vs. Debug problem? For example, is there such a thing as a static analysis tool that says "Hey, this macro or this code or this programming practice is dangerous because it has the potential to cause differences between your Debug/Release builds?"
One other "Best Practice", or rather a combination of two: Have Automated Unit Tests, and Divide and Conquer.
If you have a modular application, and each module has good unit tests, then you may be able to quickly isolate the errant piece.
The very existence of two configurations is a problem from debugging point of view. Proper engineering would be such that the system on the ground and in the air behave the same way, and achieve this by reducing the number of ways by which the system can tell the difference.
Debug and Release builds differ in 3 aspects:
_DEBUG define
optimizations
different version of the standard library
The best way around, the way I often work, is this:
Disable optimizations where performance is not critical. Debugging is more important. Most important is disable function auto-inlining, keep standard stack frame and variable reuse optimizations. These annoy debug the most.
Monitor code for dependence on DEBUG define. Never use debug-only asserts, or any other tools sensitive to DEBUG define.
By default, compile and work /release.
When I come across a bug that only happens in release, the first thing I always look for is use of an uninitialized stack variable in the code that I am working on. On Windows, the debug C runtime will automatically initialise stack variables to a know bit pattern, 0xcdcdcdcd or something. In release, stack variables will contain the value that was last stored at that memory location, which is going to be an unexpected value.
Secondly, I will try to identify what is different between debug and release builds. I look at the compiler optimization settings that the compiler is passed in Debug and Release configurations. You can see this is the last property page of the compiler settings in Visual Studio. I will start with the release config, and change the command line arguments passed to the compiler one item at a time until they match the command line that is used for compiling in debug. After each change I run the program and reproducing the bug. This will often lead me to the particular setting that causes the bug to happen.
A third technique can be to take a function that is misbehaving and disable optimizations around it using the pre-processor. This will allow you run the program in release with the particular function compiled in debug. The behaviour of the program which has been built in this way will help you learn more about the bug.
#pragma optimize( "", off )
void foo() {
return 1;
}
#pragma optimize( "", on )
From experience, the problems are usually stack initialization, memory scrubbing in the memory allocator, or strange #define directives causing the code to be compiled incorrectly.
The most obvious cause is simply the use of #ifdef and #ifndef directives associated DEBUG or similar symbol that change between the two builds.
Before going down the debugging road (which is not my personal idea of fun), I would inspect both command lines and check which flags are passed in one mode and not the other, then grep my code for this flags and check their uses.
One particular issue that comes to mind are macros:
#ifdef _DEBUG_
#define CHECK(CheckSymbol) { if (!(CheckSymbol)) throw CheckException(); }
#else
#define CHECK(CheckSymbol)
#endif
also known as a soft-assert.
Well, if you call it with a function that has side effect, or rely on it to guard a function (contract enforcement) and somehow catches the exception it throws in debug and ignore it... you will see differences in release :)
When debug and release differ it means:
you code depends on the _DEBUG or similar macros (defined when compiling a debug version - no optimizations)
your compiler has an optimization bug (I seen this few times)
You can easily deal with (1) (code modification) but with (2) you will have to isolate the compiler bug. After isolating the bug you do a little "code rewriting" to get the compiler generate correct binary code (I did this a few times - the most difficult part is to isolate the bug).
I can say that when enabling debug information for release version the debugging process works ... (though because of optimizations you might see some "strange" jumps when running).
You will need to have some "black-box" tests for your application - valgrind is a solution in this case. These solutions help you find differences between release and debug (which is very important).
The best solution is to set up something like automated unit testing to thoroughly test all aspects of the application (not just individual components, but real world tests which use the application the same way a regular user would with all of the dependencies). This allows you to know immediately when a release-only bug has been introduced which should give you a good idea of where the problem is.
Good practice to actively monitor and seek out problems beats any tool to help you fix them long after they happen.
However, when you have one of those cases where it's too late: too many builds have gone by, can't reproduce consistently, etc. then I don't know of any one tool for the job. Sometimes fiddling with your release settings can give a bit of insight as to why the bug is occurring: if you can eliminate optimizations which suddenly make the bug go away, that could give you some useful information about it.
Release-only bugs can fall into various categories, but the most common ones (aside from something like a misuse of assertions) is:
1) Uninitialized memory. I use this term over uninitialized variables as a variable may be initialized but still be pointing to memory which hasn't been initialized properly. For this, memory diagnostic tools like Valgrind can help.
2) Timing (ex: race conditions). These can be a nightmare to debug, but there are some multithreading profilers and diagnostic tools which can help. I can't suggest any off the bat, but there's Coverity Integrity Manager as one example.

Visual C++ - Why bother with Debug Mode?

So I have just followed the advice in enabling debug symbols for Release mode and after enabling debug symbols, disabling optimization and finding that break-points do work if symbols are complied with a release mode, I find myself wondering...
Isn't the purpose of Debug mode to help you to find bugs?
Why bother with Debug mode if it let bugs slip past you?
Any advice?
In truth there is no such thing as a release mode or a debug mode. there are just different configurations with different options enabled. Release 'mode' and Debug 'mode' are just common configurations.
What you've done is to modify the release configuration to enable some options which are usually enabled in the debug configuration.
Enabling these options makes the binaries larger and slower according to which ones you enable.
The more of these options you enable, the easier it is to find bugs. I think your question should really be "Why bother with release mode?" The answer to that is that it's smaller and faster.
Debug mode doesn't "let bugs slip past you". It inserts checks to catch a large number of bugs, but the presence of these checks may also hide certain other bugs. All the error-checking code catches a lot of errors, but it also acts as padding, and may hide subtle bounds errors.
So that in itself should be plenty of reason to run both. MSVC performs a lot of additional error checking in Debug mode.
In addition, many debug tools, such as assert rely on NDEBUG not being defined, which is the case in debug builds, but not, by default, in release builds.
Optimisations will be turned off, which makes debugging easier (otherwise code can be reordered in strange ways). Also conditional code such as assert() etc. can be included.
Apart from your application being very debuggeable in release mode, the MSVC runtime libraries aren't so nice, neither are some other libraries.
The debug heap, for instance, adds no-man's land markers around allocated memory to trap buffer overruns. The standard library used by MSVC asserts for iterator validity. And much more.
Bugs due to optimisers aren't unheard of; but normally they hint at a deeper problem, for instance, not using volatile when you need to will cause optimiser bugs (optimising away comparisons and using cached results instead).
At the end of the day, including debug symbols in early releases can help you trace down bugs after deployment.
Now, to answer your questions directly:
Debug mode has other things, like assert()s which are stripped in release mode.
Even if you do all your testing in release mode, bugs will still slip by. Infact, some bugs (like the above volatile bug) are only hidden in debug mode: they still exist, they are just harder to trigger.
Including full symbols with your application includes significant information about the build machine (paths etc. are embedded).
The advice is to include "PDB Only" symbols with release builds (don't include file, line and local variable symbols) with optimisation on. And debug build has no optimisation and full symbols.
And (as noted in other answer) common sub-expression elimination and instructin reordering can make debugging interesting (move next goes to lines n, n+2, n+1...).
optimization is a nightmare for debugging. Once i had an application with this for loop
for (int i = 0; i < 10; i++)
{
//use i with something
}
i was always 0 in debugging. but outputting it to a console showed that it did increase

How to make MSVC debug builds run faster

We have a large C++ application, which sometimes we need to run as a debug build in order to investigate bugs. The debug build is much much slower than the release build, to the point of being almost unusable.
What tricks are available for making MSVC Debug builds execute faster without sacrificing too much on the debugability?
Use #pragma optimize("", off) at the top of selected files that you want to debug in release. This gives better stack trace/variable view.
Works well if it's only a few files you need to chase the bug in.
Why don't you just switch on debug information in your release configuration?
We turned off Iterator debugging with the preprocessor symbols:
_HAS_ITERATOR_DEBUGGING=0
_SCL_SECURE=0
It helped a bit, but was still not as fast as we'd like. We also ended up making our debug build more release-like by defining NDEBUG instead of _DEBUG. There were a couple other options that we changed too, but I'm not remembering them.
Its unfortunate that we needed to do all this, but our application has a certain amount of work needed to be done every 50ms or its unusable. VS2008 out of the box would give us ~60ms times for debug and ~6ms times for release. With the tweaks mentioned above we could get debug down to ~20ms or so, which is at least usable.
profile the app and see what ti taking the time. you should then be able to see what debugging need to be tuned.
Are you using MFC?
In my experience, the main thing that can make a debug version slow is the class validation routines, which are usually disabled in release. If the data structure is at all tree-like, it can end up re-validating subtrees hundreds of times.
Regardless, if it is, say, 10 times slower than the release build, that means it is spending 1/10 of its time doing what's necessary, and 9/10 doing something else. If, while you're waiting for it, you just hit the "pause" button and look at the call stack, chances are 9/10 that you will see exactly what the problem is.
It's a quick & dirty, but effective way to find performance problems.
Create a ReleaseWithSymbols configuration, that defines NDEBUG and has no optimisations enabled. This will give you better performance while maintaining accurate symbols for debugging.
there are several difference between debug builds and release builds that influence both debugability and speed. The most important are the _DEBUG/NDEBUG define, the compiler optimizations and the creation of debug information.
You might want to create a third Solution Configuration and play around with these settings. For example, adding debug information to a release build doesn't really decrease performance but you already get a sensible stack trace so you know which function you are in. Only the line information is not reliable because of the compiler optimizations.
If you want reliable line information, go on and turn off optimizations. This will slow down the execution a bit but this will still be faster than normal debug as long as the _DEBUG define is not set yet. Then you can do pretty good debugging, only everything that has "#ifdef _DEBUG" or similar around it won't be there (e.g. calls to assert etc.).
Hope this helps,
Jan
Which VS are you using? We moved from VS.net to VS2008 recently and I experienced same slowness while debugging on high end machine on > 500k LOC project. Turns out, Intellisense base got corrupted and would update itself constantly but get stuck somewhere. Deleting .ncb file solved the problem.