-2

In C++, compiler optimization may produce "unexpected" program behaviors by taking advantage of undefined behaviors in a program, like signed integer overflows and dereferencing null pointers. It is important to test a program in release build if a release build is used in the production.

In C#, undefined behaviors are rare. Are there more reasons to test a program in release build before using in production other than

  1. Unsafe code
  2. Multithread timing
  3. Debug/release mode enabled/disabled code (e.g. #if)

Can optimization produce different program behaviors like C++?

keithyip
  • 985
  • 7
  • 21
  • For example in UWP projects compilation in release is much different then compilation in debug (because of. NET native tool chain), and you should always test release version. There are also switches like `#if DEBUG` that you should be aware of. I would say that it is always a good practice to test release version. – Kedrzu Aug 04 '17 at 04:14
  • The [stack may collapse due to optimization](https://www.hanselman.com/blog/ReleaseISNOTDebug64bitOptimizationsAndCMethodInliningInReleaseBuildCallStacks.aspx). The linked article about [tail call optimization](http://blogs.msdn.com/davbr/pages/tail-call-jit-conditions.aspx) mention various condition that will prevent optimization in effort to prevent behavior difference, but if your app rely on call stack condition in uncovered scenario, you will get different behavior – Martheen Aug 04 '17 at 04:23
  • https://thedailywtf.com/articles/nature-in-its-volatility – Hans Kesting Aug 04 '17 at 14:35

1 Answers1

1

This is related to multi-thread timing: in Release mode, the compiler may perform certain optimizations that change the runtime behavior of code (including introducing infinite loops in seemingly correct code) by rearranging instructions or replacing values with constants. Here is a related question about volatile vs non-volatile memory reads, and as Hans pointed out in the comments, an article from The Daily WTF about the C# compiler replacing variables with constant values as an optimization.

Jonathan Tyson
  • 463
  • 4
  • 9
  • I think that you mean "lacking memory synchronizations". Violatile is bad for this. Lock is much better generally. – keithyip Aug 07 '17 at 10:16