Unity 4.6, run in debug mode - c#

How can I run the current game inside the Unity editor in debug mode - as in preprocessor variable DEBUG will be true/enabled for scripts. I normally have a lot of:
#if DEBUG
dostuffonlyindebuglikegetalotofmoneyingame();
#else
// release mode
#endif
scattered around my projects. Using Unity 4.6 RC3.

You can have global #defines with one of these methods:
build settings, per platform
also available via build script:
PlayerSettings.SetScriptingDefineSymbolsForGroup
Global Custom Defines
For debug, maybe a check for UNITY_EDITOR is enough?

I found this:
http://docs.unity3d.com/460/Documentation/ScriptReference/Debug-isDebugBuild.html
Not sure yet but it seems not very good because:
As this is a runtime check, it would probably get debug code into release builds... affecting performance as well as stability and your reputation ;-)
The only supported mode in the Unity "Game" mode is the Debug one. This really sucks. Deploying to devices to test release builds just takes too long time because deployment is SLOW, at least on my box. :-(
Any #if DEBUG etc you wrote so far can't be used. You would have to replace that with the runtime calls Debug.isDebugBuild ... not pretty.
Not sure if Debug.isDebugBuild is removed on release builds or not... if not, Unity sucks :-)
That said, this might be the only option... poor us.

Related

Force Compile Error in Release Build but not Debug Build in C#

We have a situation where a piece of code we've written fails against a test instance of our database because of work yet undone in the database itself. (It's a patching issue on an AS/400 that is completely out of our control) We have to move forward with development and can using a hack for the code in question. We need to make sure that this hack never makes it into the production environment and were wondering if there was a way to make code build correctly when compiling for the Debug configuration but fail with a compiler error when the Release configuration is being built? I've started looking into the build configuration but have yet to find a good approach. Any ideas would be appreciated.
We're using VS 2008, C#, .Net 3.5
Use compiler directives like this:
#if !DEBUG
#error Must not make it into production
#endif
Ideally you would like to add a more complete comment describing why you can't let the hack go into production, detailing the steps or cases in your issue tracker that needs to be completed before it is safe, etc. etc.

Remove Log before release System.Diagnostics

To Log output I am using the following code.
System.Diagnostics.Debug.WriteLine("Hello");
Now, its always advisory to remove such logs before the app is submitted.
So, is that we have to remove such lines, before we submit it for release or its done implicitly.
Is there any another better way to log output in C#, which removes the logging to the console when its released. I see Log4Net is one of them.
All methods on the System.Diagnostics.Debug class have the ConditionalAttribute, so under most compilers they will not be compiled into a Release build (unless you define the DEBUG attribute in the release build). 1
This is certainly true for the compilers within Visual Studio.
Your second question about log4Net is actually the reverse, and something to be careful about if you do decide to start using log4Net - log4Net debug calls are included within debug builds and are emitted if you have the logger set to the debug trace level (usually done with runtime configuration).
1. The MSDN pages are actually (IMO) a little bit unclear, but these SO posts agree with my interpretation:
System.Diagnostics.Debug.WriteLine in production code
C# Do Debug statements get compiled out when running in Release mode?
You can use preprocessor directive:
#if DEBUG
System.Diagnostics.Debug.WriteLine("Hello");
#endif
That line will be skipped when you'll build your application in Release build configuration.

Determine debug/release mode in published DLL? Without #DEBUG

I have a component which can be referenced in some projects (for example, Component.dll). I publish it, of course, in release mode.
In another project (for example, Project.exe) I reference Component.dll.
If I build Project.exe in Debug mode, is there a way to find out about that in my Component.dll library?
To clarify more: if I have a class and a method named Test within Component.dll. Can I do something like:
public void Test(){
if(Debug.IsInDebugMode)
...
}
Keep in mind that Component.dll is built in release mode.
Whether your code is built in Release or Debug mode doesn't matter a great deal. The generated IL is very nearly the same. The Debug version will have an attribute that the jitter uses to set compilation defaults, that attribute is missing in yours. The next thing that matters is exactly how you debug or run your application. The setting that's important is Tools + Options, Debugging, General, "Suppress JIT optimization on module load". It is ticked by default.
Which now makes it matter whether your app gets started by a debugger or not. That's easy to find out, use the System.Diagnostics.Debugger.IsAttached property. When false, the machine code generated from your IL is going to be optimized by the jitter. A degenerate case is attaching a debugger after the code got started. Kinda important that this doesn't make any difference to you btw.

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!

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