views:

473

answers:

3

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.

+5  A: 

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.

fatcat1111
Agreed, it's unlikely .NET optimisation is killing your code but you may have pragma conditionals in your code that are changing the execution path and therefore the behaviour.
TreeUK
+1  A: 

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.

tommieb75
+6  A: 

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:

http://stackoverflow.com/questions/2342396/why-does-the-calculation-give-different-results-on-different-machines/2343351#2343351

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.

Eric Lippert
Maybe this thread is a good example: http://stackoverflow.com/questions/2225503
Hans Passant