Overflow or underflow in the arithmetic operation. XAML + WPF + Delphi

I came across a very strange exception. Cannot create instance of ‘ScrollViewer’ defined in assembly ‘PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’. The type initializer for ‘System.Windows.Controls.ScrollViewer’ threw an exception. Overflow or underflow in the arithmetic operation I was not doing any calculations so I knew it was not part of my application but yet is caused one of my applications to crash as soon as its executed from another application.

Context: Im busy building a UI that must be displayed from our current C# application and from our legacy Delphi application. I created a com interface for the .Net assembly which then is used by the Delphi application as a normal com object.
The problem came when we created a test application that called the method that was exposed by the com interface. As soon as the method was executed I got the following error: ‘Maximized’ value cannot be assigned to property ‘WindowState’ of object ‘Project.SomeView’. Overflow or underflow in the arithmetic operation.

While debugging the problem I found the root cause was the Overflow or underflow in the arithmetic operation exception. This exception came from inside the Microsoft PresentationFramework assembly I did not know what to do (Ive never seen anything like this) so as any other developer would I fired up Firefox and goggled Overflow or underflow in the arithmetic operation exception

I found a post that had the code below but it was completely unrelated to WPF or Delphi.
While looking at the code I thought to myself that sure cant be the answer to the problem Im having. But decided to implement the function in any case just to see what will happen.

[DllImport( “msvcr70.dll”, CallingConvention = CallingConvention.Cdecl )]
public static extern int _fpreset();

I did some research on the API and found that it resets the floating point package. More information about the API can be found here. In my constructor just before the InitializeComponent() method I’ve added the following line

// Reset the Floating Point
_fpreset();

So in the end my constructor looked like this:

public SomeView()
{  

// Reset the Floating Point (When called from External Application there was an Overflow exception)
_fpreset();  

// Initialize the Component
InitializeComponent();
}

I’ve posted a the same solution here

Hope this helps anyone in the future.

Cheers

Advertisements

About Johan

.Net Developer
This entry was posted in WPF. Bookmark the permalink.

3 Responses to Overflow or underflow in the arithmetic operation. XAML + WPF + Delphi

  1. Nathan Seeley says:

    Thank you! I needed this solution and it works perfectly. I can’t imagine how many hours you struggled with this problem, but I appreciate that you shared your solution.

  2. Pingback: WPF – валится просто при выборе TextBox… :- | Dmitry-Bond

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s