C# XNA Visual Studio: Difference between "release" and "debug" modes? - c#

I'm working on a demo about collision detection. (Some of the code for this is detailed here.) In Debug mode, it works fine. In Release mode, it's faster, but the collision detection is REALLY messed up. Objects to bounce off nothing, or seem be oddly lightly effected by gravity. Some objects explode, as if they have collided with the special explosive objects, even though none of those objects exist.
So... what does Visual Studio change between Release and Debug mode that causes this problem? (I'm using VS Pro 2008.)
Mysteriously, Release mode had been working for plenty of development. It just recently stopped.

My psychic powers are not great, and it is hard to tell what is going on without actually debugging it. But here's a guess. The issue I discuss here:
Why does this floating-point calculation give different results on different machines?
applies not just to "cross machine" but also to "debug vs release". It is not only possible but likely that the release version of your program is using higher precision math than your debug version. If you have floating point bugs in there then it is entirely possible that just by sheer bad luck you are hitting the bugs only in the higher-precision release version and not in the lower-precision debug version.
Why the difference? Because in the unoptimized version the C# compiler frequently generates code for temporary values as though they were local variables; the jitter then actually allocates temporary locals on the stack, and writes the temporary values from the registers to the locals. Then when it needs them, it reads them back into registers from the temporaries. That journey can cause the value that was in the high-precision register to be truncated to mere 64 bit precision, losing bits of precision.
In the optimized version the C# compiler and the jitter work harder to keep everything in registers all the time, because obviously that is faster and higher precision, though harder to debug.
Good luck. Bugs that only repro in release mode are a total pain.

First, any #if(DEBUG) or #if(RELEASE) pragmas are entered. You may have code in one or the other that should or shouldn't be called, so search for those.
Beyond that, by default Release builds are set to "optimize code", while Debug isn't. Try changing that setting in your release configuration (Project > Properties > Build > "Optimize code") and seeing if that solves the problem.

With debug mode, there is a define in place, (you can confirm this by checking the 'Build Options' when you right click on the solution and select 'Properties' which if used, you can issue the trace calls. In release mode, the define is removed and hence no debugging information is used. If you were to debug that release code, the debugger will not be able to tell what line (original code) it was on even if you specified the location of the source as the code is optimized.
As for your situation, maybe by clearing out the intermediate build files in the release directory or remove the .suo file found in the solution directory might help.
Hope this helps,
Best regards,
Tom.

Related

what's the difference between C# compilation setting "/debug:pdbonly" and "/debug:full"? [duplicate]

In Visual Studio for a C# project, if you go to Project Properties > Build > Advanced > Debug Info you have three options: none, full, or pdb-only.
Which setting is the most appropriate for a release build?
So, what are the differences between full and pdb-only?
If I use full will there be performance ramifications? If I use pdb-only will it be harder to debug production issues?
I would build with pdb-only. You will not be able to attach a debugger to the released product, but if you get a crash dump, you can use Visual Studio or WinDBG to examine the stack traces and memory dumps at the time of the crash.
If you go with full rather than pdb-only, you'll get the same benefits, except that the executable can be attached directly to a debugger. You'll need to determine if this is reasonable given your product & customers.
Be sure to save the PDB files somewhere so that you can reference them when a crash report comes in. If you can set up a symbol server to store those debugging symbols, so much the better.
If you opt to build with none, you will have no recourse when there's a crash in the field. You won't be able to do any sort of after-the-fact examination of the crash, which could severely hamper your ability to track down the problem.
A note about performance:
Both John Robbins and Eric Lippert have written blog posts about the /debug flag, and they both indicate that this setting has zero performance impact. There is a separate /optimize flag which dictates whether the compiler should perform optimizations.
WARNING
MSDN documentation for /debug switch (In Visual Studio it is Debug Info) seems to be out-of-date! This is what it has which is incorrect
If you use /debug:full, be aware that there is some impact on the
speed and size of JIT optimized code and a small impact on code
quality with /debug:full. We recommend /debug:pdbonly or no PDB for
generating release code.
One difference between /debug:pdbonly and /debug:full is that with
/debug:full the compiler emits a DebuggableAttribute, which is used to
tell the JIT compiler that debug information is available.
Then, what is true now?
Pdb-only – Prior to .NET 2.0, it helped to investigate the crash dumps from released product (customer machines). But it didn't let attaching the debugger. This is not the case from .NET 2.0. It is exactly same as Full.
Full – This helps us to investigate crash dumps, and also allows us to attach debugger to release build. But unlike MSDN mentions, it doesn't impact the performance (since .NET 2.0). It does exactly same as Pdb-only.
If they are exactly same, why do we have these options? John Robbins (windows debugging god) found out these are there for historical reasons.
Back in .NET 1.0 there were differences, but in .NET 2.0 there isn’t.
It looks like .NET 4.0 will follow the same pattern. After
double-checking with the CLR Debugging Team, there is no difference at
all.
What controls whether the JITter does a debug build is the /optimize
switch. <…>
The bottom line is that you want to build your release builds with
/optimize+ and any of the /debug switches so you can debug with source
code.
then he goes on to prove it.
Now the optimization is part of a separate switch /optimize (in visual studio it is called Optimize code).
In short, irrespective of DebugInfo setting pdb-only or full, we will have same results. The recommendation is to avoid None since it would deprive you of being able to analyze the crash dumps from released product or attaching debugger.
You'll want PDB only, but you won't want to give the PDB files to users. Having them for yourself though, alongside your binaries, gives you the ability to load crash dumps into a debugger like WinDbg and see where your program actually failed. This can be rather useful when your code is crashing on a machine you don't have access to.
Full debug adds the [Debuggable] attribute to your code. This has a huge impact on speed. For example, some loop optimizations may be disabled to make single stepping easier. In addition, it has a small effect on the JIT process, as it turns on tracking.
I'm in the process of writing a unhandled exception handler and the stack trace includes the line number when pdb-only is used, otherwise I just get the name of the Sub/Function when I choose None.
If I don't distribute the .pdb I don't get the line number in the stack trace even with the pdb-only build.
So, I'm distributing (XCOPY deploy on a LAN) the pdb along with the exe from my VB app.

Should I compile release builds with debug info as "full" or "pdb-only"?

In Visual Studio for a C# project, if you go to Project Properties > Build > Advanced > Debug Info you have three options: none, full, or pdb-only.
Which setting is the most appropriate for a release build?
So, what are the differences between full and pdb-only?
If I use full will there be performance ramifications? If I use pdb-only will it be harder to debug production issues?
I would build with pdb-only. You will not be able to attach a debugger to the released product, but if you get a crash dump, you can use Visual Studio or WinDBG to examine the stack traces and memory dumps at the time of the crash.
If you go with full rather than pdb-only, you'll get the same benefits, except that the executable can be attached directly to a debugger. You'll need to determine if this is reasonable given your product & customers.
Be sure to save the PDB files somewhere so that you can reference them when a crash report comes in. If you can set up a symbol server to store those debugging symbols, so much the better.
If you opt to build with none, you will have no recourse when there's a crash in the field. You won't be able to do any sort of after-the-fact examination of the crash, which could severely hamper your ability to track down the problem.
A note about performance:
Both John Robbins and Eric Lippert have written blog posts about the /debug flag, and they both indicate that this setting has zero performance impact. There is a separate /optimize flag which dictates whether the compiler should perform optimizations.
WARNING
MSDN documentation for /debug switch (In Visual Studio it is Debug Info) seems to be out-of-date! This is what it has which is incorrect
If you use /debug:full, be aware that there is some impact on the
speed and size of JIT optimized code and a small impact on code
quality with /debug:full. We recommend /debug:pdbonly or no PDB for
generating release code.
One difference between /debug:pdbonly and /debug:full is that with
/debug:full the compiler emits a DebuggableAttribute, which is used to
tell the JIT compiler that debug information is available.
Then, what is true now?
Pdb-only – Prior to .NET 2.0, it helped to investigate the crash dumps from released product (customer machines). But it didn't let attaching the debugger. This is not the case from .NET 2.0. It is exactly same as Full.
Full – This helps us to investigate crash dumps, and also allows us to attach debugger to release build. But unlike MSDN mentions, it doesn't impact the performance (since .NET 2.0). It does exactly same as Pdb-only.
If they are exactly same, why do we have these options? John Robbins (windows debugging god) found out these are there for historical reasons.
Back in .NET 1.0 there were differences, but in .NET 2.0 there isn’t.
It looks like .NET 4.0 will follow the same pattern. After
double-checking with the CLR Debugging Team, there is no difference at
all.
What controls whether the JITter does a debug build is the /optimize
switch. <…>
The bottom line is that you want to build your release builds with
/optimize+ and any of the /debug switches so you can debug with source
code.
then he goes on to prove it.
Now the optimization is part of a separate switch /optimize (in visual studio it is called Optimize code).
In short, irrespective of DebugInfo setting pdb-only or full, we will have same results. The recommendation is to avoid None since it would deprive you of being able to analyze the crash dumps from released product or attaching debugger.
You'll want PDB only, but you won't want to give the PDB files to users. Having them for yourself though, alongside your binaries, gives you the ability to load crash dumps into a debugger like WinDbg and see where your program actually failed. This can be rather useful when your code is crashing on a machine you don't have access to.
Full debug adds the [Debuggable] attribute to your code. This has a huge impact on speed. For example, some loop optimizations may be disabled to make single stepping easier. In addition, it has a small effect on the JIT process, as it turns on tracking.
I'm in the process of writing a unhandled exception handler and the stack trace includes the line number when pdb-only is used, otherwise I just get the name of the Sub/Function when I choose None.
If I don't distribute the .pdb I don't get the line number in the stack trace even with the pdb-only build.
So, I'm distributing (XCOPY deploy on a LAN) the pdb along with the exe from my VB app.

Unit testing on a build server : Release or Debug code?

In .NET(C#) is there any advantage/disadvantage to go with debug/release build for unit testing?
Which target configuration do you usually use for unit testing on a build server? Does it matter?
What about code coverage (for this one I'm guessing debug versions are needed).
I'd recommend running the release code. For a couple of reasons.
1) It is the code that the customers will be using.
2) Some code has special debug conditionals that will produce differences between the debug and release builds.
You must test the code the way it will ultimately run on the client's machine. In most sane deployment scenarios that will be code compiled in the Release configuration.
I would use release build when possible, to get everything as close to the final product as possible.
There are small differences between debug mode and release mode that normally only make a difference for performance, but not result. However, if there is some timing problems with the code they may only show in release mode, so you could take the opportunity to possibly catch those.
Despite most people obviously favour to unit test the release code, I wonder whether the debug build might uncover more errors, though. (I might be wrong)
E.g. afaik in VS debug code, uninitialized variables are forced to some awful value instead of being 0 "by accident". Maybe in .NET it does not make a big difference, but for me, doing mainly algorithmic core code in C++, this can be crucial.
I look forward to any enlightening comments ;).
It is easier (for me) to get to the root of an exception when testing debug code.
Just adding another reason to test in release mode. Some CI services (Appveyor) will fail the build if it comes across a Debug.WriteLine() call, even though the test itself is green.
We are running both, Debug + Release.
We output a separate tests results xml for each build.
Sometimes there are errors in Debug only, and sometimes in Release only, you want to catch them all ASAP.
Good luck!

understanding a Build c++

I think I know what a build is. But I am not sure. My definition of a build is another word for saying compiled application. Can someone please tell me what exactly a build is. And why do people ask for 3 types of builds. Such as Debug Build, Profile Build and a Release Build. What are the differences.
[edit]
the types of builds
Have a look at Visual Studio Debug and Release Modes
Release Mode
When an assembly is built in release mode, the compiler performs all available optimisations to ensure that the outputted executables and libraries execute as efficiently as possible. This mode should be used for completed and tested software that is to be released to end-users. The drawback of release mode is that whilst the generated code is usually faster and smaller, it is not accessible to debugging tools.
Debug Mode
Debug mode is used whilst developing software. When an assembly is compiled in debug mode, additional symbolic information is embedded and the code is not optimised. This means that the output of the compiler is generally larger, slower and less efficient. However, a debugger can be attached to the running program to allow the code to be stepped through whilst monitoring the values of internal variables.
A build means basically doing a set of tasks to make your program. The main components of a typical build is compiling and linking.
More specifically a build can contain compiling, linking, setting version numbers, copying outputs to some location, creating an installer and anything else.
When people say debug or release build or etc., they may have different settings defined for each. For example in a debug build you will create program database files for debugging.
A build does not have to include only compiled and linked targets. Usually there is at least one of those, but a "build" could also include creating plain-text or binary files, moving images, sounds, and other files into the correct places to be accessed by the file, or any other operation that needs to be performed for the application to run.
The multiple types of builds are made to target different "audiences", if you will. For instance, and end-user does not need to collect information about what functions were called or how many times and exception was raised, or any other diagnostic info (though that information is valuable to developers). Usually the final "release" build is made to be fast and small, and not load the user down with extras like that.

Debug vs. release in .NET

Continuing from my previous question, is there a comprehensive document that lists all available differences between debug and release modes in a C# application, and particularly in a web application?
What differences are there?
"Debug" and "Release" are just names for predefined project configurations defined by Visual Studio.
To see the differences, look at the Build Tab in Project Properties in Visual Studio.
The differences in VS2005 include:
DEBUG constant defined in Debug configuration
Optimize code enabled in Release configuration
as well as other differences you can see by clicking on the "Advanced" button
But you can:
Change the build settings for Debug and Release configurations in Project Propeties / Build
Create your own custom configurations by right-clicking on the solution in Solution Explorer and selecting Configuration Manager
I think the behaviour of the DEBUG constant is fairly clear (can be referenced in the #if preprocessor directive or in the ConditionalAttribute). But I'm not aware of any comprehensive documentation on exactly what optimizations are enabled - in fact I suspect Microsoft would want to be free to enhance their optimizer without notice
I'm not aware of one concise document, but:
Debug.Write calls are stripped out in Release
In Release, your CallStack may look a bit "strange" due to optimizations, as outlined by Scott Hanselman
There isn't one document that lists the differences. In addition to some of the differences already listed, compiling in Debug mode turns off most of the JIT compiler optimizations that are performed at runtime and also emits more complete debug information in to the symbol database file (.pdb).
Another big difference is that the GC behavior is somewhat different in that the JIT compiler will insert calls to GC.KeepAlive() as appropriate/needed in order to support debugging sessions.
Debug and Release are just labelling for different solution configurations. You can add others if you want. If you wish you can add more configurations from configuration manager–
http://msdn.microsoft.com/en-us/library/kwybya3w.aspx
Major differences –
In a debug DLL several extra instructions are added to enable you to set a breakpoint on every source code line in Visual Studio. Also, the code will not be optimized, again to enable you to debug the code.
In the release version, these extra instructions are removed.
PDB file is created in only Debug mode and not in release mode.
In release mode, code is optimized by the optimizer that's built into the JIT compiler. It makes the following optimizations:
• Method inlining - A method call is replaced by the injecting the code of the method.
• CPU register allocation - Local variables and method arguments can stay stored in a CPU register without ever (or less frequently) being stored back to the stack frame
• Array index checking elimination - An important optimization when working with arrays (all .NET collection classes use an array internally). When the JIT compiler can verify that a loop never indexes an array out of bounds then it will eliminate the index check.
• Loop unrolling - Short loops (up to 4) with small bodies are eliminated by repeating the code in the loop body.
• Dead code elimination - A statement like if (false) { /.../ } gets completely eliminated.
• Code hoisting- Code inside a loop that is not affected by the loop can be moved out of the loop.
• Common sub-expression elimination. x = y + 4; z = y + 4; becomes z = x
One major performanance area if you are using any of the ASP.NET Ajax controls: debug information is removed from the JavaScript library when running in release, and I have seen major preformance improvements on complicated pages. Other web based resources may be either cached or not cached based on this setting.
Also, remember that Debug / Release in a web application is dictated by the web.config file, not your settings within Visual Studio.
<system.web>
<compilation debug="true">
More information:
Don’t run production ASP.NET Applications with debug=”true” enabled
You can also manage some part of code that you want to run only in debug or only in release with preprocessor markups:
#if DEBUG
// Some code running only in debug
#endif
or
#if NOT DEBUG
// Some code running only in release
#endif
Drawing with GDI+ is considerably slower in Debug mode.
Release version:
is considerable faster (most important), optimized
can't be debuged (step by step)
and code written in "debug" directive is not included
See What's the difference between a Debug vs Release Build?.
I got an error message when I distribute executable file to another machine indicating that the system missed MSVCP110D.dll.
The solution to this issue is stated in Stack Overflow question Visual Studio MSVCP110D.dll is missing.
IN XXXXD.dll D means that the DLL file is a debug version of the DLL file. But MS Visual C++ Redistributable packages include only the release version of DLL files.
That means if you need to distribute a program developed by Visual C++ you need to build it in Release mode. And also you need to install MS Visual C++ Redistributable (correct version) on the target machine.
So I think this a one of key difference between debug and release mode.

Categories