title: Arithmetic underflow or overflow exception during debugging
body: <p>This is the day of weird behavior.</p>
  <p>We have a Win32 project made with Delphi 2007, which hosts the .NET runtime and calls into .NET to show new forms, as part of a transition period.</p>
  <p>Recently we've begun experiencing exceptions at seemingly random locations and points of our code: Arithmetic overflow or underflow.</p>
  <p>The stack trace of one of these looks like this:</p>
  <pre><code>at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
  at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
  at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
  at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
  at System.Windows.Forms.Application.RunDialog(Form form)
  at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
  at System.Windows.Forms.Form.ShowDialog()
  at Gatsoft.Gat.UI.Windows.Forms.Remanaging.RemanageForm.DelphiOpenInNewMode(String employeeCode, String departmentCode, DateTime date) in C:\Dev\VS.NET\Gatsoft\Gatsoft.Gat.UI.Windows\Forms\Remanaging\RemanageForm.Delphi.cs:line 67
  </code></pre>
  <p>In the Visual Studio solution, one of the outmost class libraries (ie. pulls in all the references it can), has set a specific debug program, targetted for the Delphi project output. This allows us to debug .NET code from Visual Studio, even though the main bulk of the program is written in Delphi.</p>
  <p>The problem only occurs when run from the debugger, not if we just run the exe file directly (either through explorer, shortcuts, or even <kbd>Ctrl</kbd>+<kbd>F5</kbd> inside Visual Studio).</p>
  <p>There's apparently no spyware on the machine (as hinted by <a href="http://bytes.com/forum/thread106203.html" rel="nofollow noreferrer">this</a>).</p>
  <p>Any other things we can check?</p>
  <hr />
  <p><strong>Edit:</strong> It looks like the .NET debugger is enabling this SNaN flags, and the Delphi debugger does not. We'll have to investigate this further, but for now I'll accept <a href="https://stackoverflow.com/users/6550/lorenzo-boccaccia">@Lorenzo Boccaccia</a>'s answer.</p>
  <h2>Apparently Solved</h2>
  <p>Ok, it looks like we've finally nailed this problem. The problem started occuring without having the debugger attached as well, for our testers, so we had to prioritize the problem way up.</p>
  <p>Finally we found one common issue with the machines that had the problem, they are Dell Lattitude D620 laptops with an NVIDIA Quadro NVS 110M, with an old driver from a system image used to provision the laptops, from back in 2006.</p>
  <p>I found one post on the web, though I lost the url when I rebooted to update the display driver, that had a .NET service crashing, mostly when the machine was busy doing something on the screen. One way to reproduce his problem was to open a command prompt to C:\ and doing a <code>DIR /S</code> to just force a massive amount of screen updates, which would trigger the crash.</p>
  <p>He too had a NVIDIA video card.</p>
  <p>The problem on my machine occured roughly every 2-4 startups of our program, but after updating the video driver I've had 123 successfull startups without any problems. (BTW I can recommend <a href="http://www.autohotkey.com/" rel="nofollow noreferrer">AutoHotKey</a> for such things).</p>
  <p>So it looks like we've found the culprit, an old/buggy NVIDIA driver.</p>
  <p>Updated this question so that perhaps someone in the future can save some time.</p>
  <p>Now, if you'll excuse me, I'm going to go cry in a corner.</p>
  <h2>Jinxed!</h2>
  <p>I must've jinxed it. No sooner had I posted the above update than a colleague laptop failed, after updating the video driver.</p>
  <p>Still, I'm positive it's a problem outside of our application now, so it just remains to figure out which specific things to update.</p>
  <hr />
  <p><strong>Further updates</strong>: Ok, my machine is now apparently fixed, not so with my colleagues machine. So far we've updated the BIOS, Chipset drivers, and currently SP3 for XP is on its way in.</p>
  <p>A burn-in test will be done tonight, where the app will be left overnight starting up, as the problem cropped up either during startup, or at the first time some WinForms .NET code was executed. This app is mainly a Delphi Win32 app, but it hosts the .NET runtime, and the problem seems to be related to .NET code. When we &quot;boot&quot; the .NET runtime, the problem can appear, or when we fire the first .NET window from Win32 then it can also appear.</p>
  <hr />
  <p>Statistically I'm ready to release this code now. Over the night the application has been started 3051 times without errors, whereas before I updated the video driver it crashed every 2-4 times.</p>
  <h2>Prodded and found(!/?)</h2>
  <p>This bug-fixing ordeal feels like going to the doctor, where the following conversation ensues:</p>
  <pre><code>Doc: Does this hurt?
  Me: No...
  Doc: What about now?
  </code></pre>
  <p>I've prodded and poked the application and finally I think I've found something we did that introduced this problem.</p>
  <p>In our app we host the .NET runtime, from a Delphi 2007 Win32 application, and in our glue-code we have the following line (now):</p>
  <pre><code>  rc := CorBindToRuntimeEx('v2.0.50727', 'wks',
    STARTUP_LOADER_OPTIMIZATION_MULTI_DOMAIN or STARTUP_CONCURRENT_GC,
    @clsid, @iid, UnkRuntimeEngine);
  </code></pre>
  <p>The two constants in the middle there was originally just a 0, meaning <em>pick the defaults</em>. This change was introduced a few months ago and the problem has been slowly creeping in on us after this. The change was introduced in order to encourage ANTS profiler to load our Win32 application + hosted .NET runtime in order to do performance profiling and the changes we introduced back then made that work. Additionally, the problem with arithmetic overflow/underflow has slowly been getting worse so I bet the problem didn't appear for a while after the change so it wasn't attributed to any of the changes we did.</p>
  <p>Also, since we only (originally) saw the problem when running through the debugger, we thought something was wrong with Visual Studio and/or Delphi.</p>
  <p>Anyway, statistically now, with a browser on one screen doing repeated scrolling up and down triggered by a javascript (apparently needed in order to trigger the bug), then I have been able to successfully start the application 726 times with a 0 in the call, and it crashes 5 out of 17 times with the two constants there.</p>
  <pre><code>Doc: Does this hurt?
  </code></pre>
  <p>And let's not get into who made that change in the first place. I'm sure the culprit wants to be left anonymous... <em>cough</em></p>