Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 280
Nick Heiner Avatar asked Feb 26 '10 23:02

Nick Heiner


2 Answers

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.

like image 145
Eric Lippert Avatar answered Sep 21 '22 00:09

Eric Lippert


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.

like image 41
Justin R. Avatar answered Sep 20 '22 00:09

Justin R.