2

I have been developing an add on with C# and WPF to an application.

It's been working fine until launched on a Win7 machine. The symptoms are that a Microsoft .NET Framwork's "Unhandled exception" dialog pops up on startup for System.ArithmeticException (Overflow or underflow in the arithmetic operation) and gives a stack trace pointing to System.Windows.Controls.Primitives.Track.ComputeScrollBarLengths (...) and deeper.

So, I began to debug the app: it showed that System.ArithmeticException was thrown when setParent from user32.dll was called. This is done when the application makes a call to show the add-on UI.

public bool ShowUI(int Parent)
{
userControl = new MyUserControl(); // Extends System.Windows.Forms.UserControl
SetParent(userControl.Handle, new IntPtr(Parent)); // <- exception thrown here
...
}

What would possibly be causing this issue?

am9417
  • 994
  • 8
  • 18
  • 2
    This exception is invariably caused by unmanaged code changing the floating point processor control register. WPF has a rock hard requirement that this never happens, floating point exceptions need to stay disabled. Chase it down with the debugging tips in [this post](https://stackoverflow.com/a/14466004/17034). – Hans Passant Feb 08 '18 at 14:09
  • Thanks. Apparently it is something related to it, since calling `_fpreset()` from msvcrt.dll in the add-on's bootstrapper seems to resolve this issue by now. It needs further testing, though... – am9417 Feb 09 '18 at 12:06

1 Answers1

0

I'm adding the solution here in case someone finds it userful.

Something in the unmanaged code – only in the 64-bit version, though – caused the floating point operations to cause exceptions which caused this error, as mentioned in Hans Passant's comment. The solution was something similar to this answer: https://stackoverflow.com/a/19722292/8063451

Adding the _fpreset() function call at the initialisation of my add-on solved the problem.

    [DllImport("msvcr.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int _fpreset();

    public static void Run()
    {
       // Other code
        _fpreset(); // Reinitialises the floating-point package
    }
am9417
  • 994
  • 8
  • 18