Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Compiler -- DEBUG vs. RELEASE

For years I have been using the DEBUG compiler constant in VB.NET to write messages to the console. I've also been using System.Diagnostics.Debug.Write in similar fashion. It was always my understanding that when RELEASE was used as the build option, that all of these statements were left out by the compiler, freeing your production code of the overhead of debug statements. Recently when working with Silverlight 2 Beta 2, I noticed that Visual Studio actually attached to a RELEASE build that I was running off of a public website and displayed DEBUG statements which I assumed weren't even compiled! Now, my first inclination is to assume that that there is something wrong with my environment, but I also want to ask anyone with deep knowledge on System.Diagnostics.Debug and the DEBUG build option in general what I may be misunderstanding here.

like image 911
Pete Avatar asked Aug 26 '08 19:08

Pete


People also ask

What is difference between debug and Release?

Release vs Debug and it depends on what language you are using, Debug includes debugging information in the compiled files (allowing easy debugging) while Release usually has optimizations enabled. They each define different symbols that can be checked in your program, but they are language-specific macros.

What is the difference between a debug build and a Release build?

By default, Debug includes debug information in the compiled files (allowing easy debugging) while Release usually has optimizations enabled. As far as conditional compilation goes, they each define different symbols that can be checked in your program, but they are language-specific macros.

How much faster is Release than debug?

Default Debug build is x240 times slower than default Release build. With all the aforementioned settings enabled, Fast Debug build is only x3 times slower than Release build (and that's with optimization still disabled!). The total improvement of Fast Debug over default Debug is 77x times.

Can I debug in Release mode Visual Studio?

You can now debug your release build application. To find a problem, step through the code (or use Just-In-Time debugging) until you find where the failure occurs, and then determine the incorrect parameters or code.


2 Answers

The preferred method is to actually use the conditional attribute to wrap your debug calls, not use the compiler directives. #ifs can get tricky and can lead to weird build problems.

An example of using a conditional attribute is as follows (in C#, but works in VB.NET too):

[ Conditional("Debug") ]
private void WriteDebug(string debugString)
{
  // do stuff
}

When you compile without the DEBUG flag set, any call to WriteDebug will be removed as was assumed was happening with Debug.Write().

like image 96
Mike Avatar answered Nov 04 '22 02:11

Mike


Examine the Debug.Write method. It is marked with the

[Conditional("DEBUG")]

attribute.

The MSDN help for ConditionalAttribute states:

Indicates to compilers that a method call or attribute should be ignored unless a specified conditional compilation symbol is defined.

Whether the build configuration has a label of release or debug doesn't matter, what matters is whether the DEBUG symbol is defined in it.

like image 5
Bjorn Reppen Avatar answered Nov 04 '22 02:11

Bjorn Reppen