Why would I start a debug build without debugging? - c++

Is there any benefit in starting a debug build without debugging (as opposed to a release build without debugging)? And what do I miss when I debug a release build (as opposed to debugging a debug build)?

Biggest advantages of debug builds (outside of the IDE):
Assertions are enabled, as is other diagnostic code you may have compiled within debug-dependent prepocessor sections.
Stack traces and variable watches are working properly, so you can have beta testers send you a crash dump and debug that in your IDE later.
Biggest disadvantages:
Slow execution, higher memory consumption, bigger file size.
Some bugs are not evident unless you compile everything with full optimization. That's because memory allocation is working differently in release builds.
Many companies distribute debug builds to alpha and beta testers and switch to release builds later.

To add to Adrians answer and as a general point when talking about debug vs. release builds:
Here are some factors that influence your builds:
You link against either the debug or the release runtime libs (/MD vs. /MDd)
NDEBUG (release mode) or _DEBUG (debug mode) is #defined
_SECURE_SCL (or some equivalent) is defined (or not)
Compiler optimizations are enabled (to some degree)
A "debug build" normally comprises _DEBUG, _SECURE_SCL=1, /MDd and all compiler optimizations disabled. This results in the "safest", "most-checked" execution mode, but also should be the slowest version you can get for your executable. The speed and safeness factors should be completely independent of whether you run your program under a debuger or not! -- The debug build gives you a maximum safety and error catching net, completely independent of whether the program is attached to a debugger.
Next comes a non-optimized release build: That is, you have all the release mode settings (NDEBUG, _SECURE_SCL=0, etc.), but you disable all compiler optimizations. This is good for testing, since performance won't be bogged down too much and you can debug this allright. Again, the usefulness of this is independent of whether you run your program under a debugger.
Finally come full optimizations. (/Ox + full inlining + possibly whole prg optimization): While this is what you would like to ship for performance reasons, chances are that you do not have enough people in your company that are actually able to debug this. That is, given a crash dump, someone most likely needs some amount of asm knowledge and what a compiler outputs to make some sense of the crash dump (or even some random breakpoint, when actually running under the debugger). Again, the pros/cons for full opt are independent of starting/running the prg under a debugger.

Starting a debug build without debugging can give you for example the following benefit: if a contrainer is indexed with an invalid index you'll get an assertion failed, in release mode you'll get undefined behavior. That's one thought. What you miss when debugging in release mode is that there is no correspondence between source lines and assembly code any more because the optimizer has run. So it's far more difficult to debug in release mode

I'll offer a recent experience that I can't explain -- sometimes when I run my application, I get unhandled exceptions when running in the IDE. The problem is, I know my exception is getting handled, and I also know that I'm not breaking on thrown exceptions (via CTRL-D, E). If I hit F5 several times, my error handler eventually catches the exception and deals with it properly.
The reason for this has eluded me for several weeks, so when I don't want the execution to get interrupted, I run outside of the IDE and simply attach to the process later if I need to.
If you really need to see the Debug output while running outside of the IDE and you're not using something like log4net to capture everything, you can use DebugView instead.

Related

Assertion thrown in Release but not debug - necessarily a compiler bug?

This is not a specific question about my code but a more high level question.
I have C++ code that compiles in Release and Debug mode (both with assertions turned on)
There is no code in the library that checks for Debug etc (via prerocsssor definitions or macros).
Running in debug mode doesn't cause any assertions. Running in Release or RelWithDebInfo throws an assertition every time.
Does this necessarily mean that there is a bug in the MSVC compiler/optimiser? Also, more generally: does a difference in program behaviour between Release and Debug always constitute a bug in the compiler.
Note: clang run this code fine without throwing assertions in Release and Debug.
This kind of thing is almost never a compiler bug. If you have code that overwrites data because of a stray pointer, when you compile with different options it can overwrite different data, with vastly different results.

VS2010 Express recommends optimizations in debug mode, and fails running the app without them

we have encountered a very weird behaviour of VS2010 Express C++ recently.
Our app which worked flawlessy, suddenly showed unexpected behaviour in debugmode, like somehow rendering to the full screen, despite it being a smallish editor-window.
What we found out was that the new behaviour can be disabled by switching the code-optimizations in the debugmode from "deactivated" (/Od) to "project default" which then, unexpectedly uses the "maximize speed" option (/O2).
This wouldn't be bad in particular if it weren't that debugging now is almost impossible because of the code restructuring going on, so breakpoints, stepping through it, looking at values, is just meaningless. The debugger cant even figure out the correct values of variables anymore (which is just logical).
So i'm pretty baffled what to do with it. Why does it fail in the first place, and why does it want an optimized program in the debugmode?
Also, not being funny, we encountered a c++ compiler bug, where NOT handing over a template parameter was not noticed by the compiler and so made some code silently fail.
The project defaults are not debug mode defaults, but are actually the defaults for release mode. The project defaults do not know about debug/release configurations. The override /Od is the default setting for debug configurations. If you have reset all project properties in the debug configuration to the defaults, then you have set your debug configuration to pretty much the same as release mode.
There is nothing special about the debug/release configurations - they are just names, so the project properties does not include special defaults for each one.
Think of stuff like return value optimization. Also look for temporary objects that the compiler avoids creating when code optimization is turned on. Maybe you could use some C++ lint application to smoke out some of the reasons your code is misbehaving in debug mode.

C++ 64bit, variable not found

I have a problem with my C++ application. It was developed on a 32bit pc, on Microsoft Visual Studio 2008, and now I am trying to run it on a 64bit pc.
On my 32bit pc it works fine; on the 64bit pc, Visual Studio does not give any compilation problem, but then on execution gives wrong results.
And I have undestood why.
In the code, I define a variable, of tipe "dag", that is a structure for a direct acyclic graph. By debugging the software, I noticed that, although I declared it, later the software is not able to insert data in it, and the debugger says:
CXX0017: Error: symbol "dags" not found
Here's my code:
Dag<int64_t>* dags = new Dag<int64_t>();
dags = getDagsFromRequest2(request, dags);
The very strange thing is that, if I follow the flow inside getDagsFromRequest2() function, I can clearly see that dags variable is full of data: on "quickwatch", it shows 2342 nodes inside it. But when I come back from getDagsFromRequest2() function to this part of the code, debugger says "CXX0017: Error: symbol "dags" not found". How is it possible?
You can also see this screenshot from my Visual Studio debug set.
What could be the problem?
Thanks a lot
There are a few possibilities to consider:
Running in Release builds. Switch to a Debug build.
Using a Debug build that has optimizations enabled and/or debug information disabled. Disable the optimizations and enable the debug information (look in another project for the relevant settings).
A corrupt build of some sort. Clean and rebuild the entire solution.
Memory corruption which is preventing the debugger from displaying the variable. Ensure that no memory issues exist with a tool like Valgrind.
A VS bug. This report for VS2010 seems to suggest a known bug with similar characteristics for example. Ensure all patches and hotfixes for VS2008 are installed.
The variable dags is defined as your code compiles. The error you see is simply related to the debugger. I am guessing it is caused by running the application in Release mode which sometimes causes confusing and wrong watches values. Try changing the mode to debug(there is a drop down from which you can choose the build mode).
EDIT: as you say you are running in Debug mode, my next guess is that this behavior could be caused by stack corruption. Try using valgrind to detect if that is the case. It may take a while to start with it,but it is worth it and will detect if you have some memory corruption.

C++ debug release version

Very basic question.
I want to debug release version of my exe. My debug version is working fine. But release version crashing as usual.
Any tool or debugger available for this would be a great help.
You can still enable debug information in release mode, and use a debugger as usual. Nothing particular here, except that the order of instructions will sometimes look weird when debugging due to optimizations.
Good luck, debugging release-mode-only bugs is tedious.
I'd recommend you adding some kind of logging system or trace points to locate the source of crash. The debugger can trick you when debugging release. You can also elevate the warning level of your compiler to see some usual suspects such as use of unitialized variables.
I recommend you to run exe under some memory debugger, such as Rational Purify or BoundsChecker. It will spot memory related bugs in your code, if any.

How does building a project in Debug differ with in Release?

In visual studio or any other IDE, usually there are two build configurations, Debug and Release?
How does the differ? Why sometimes you have compile errors when building in Debug mode, but not when in Release mode, and vice versa?
Debug compiles with symbols and allows you to "see" your code when it runs. It also does some initialization of variables to help in the bug tracking process.
Release is generally optimized, and does not generate debug data.
Generally when you get compile issues toggling between the two, it relates to hard coded paths to folders.
MSDN on configurations
The debug build is created with some embedded information (symbols) which allows the debugger to debug the application and exposes the runtime behavior of the application. On the down side, the debug build is a bit slower and inefficient in execution and has a bigger memory footprint.
Source:http://www.programmersheaven.com/2/FAQ-VISUALSTUDIO-Debug-Release