Thursday, August 05, 2004

A weird bug - This time I don't know where, but solved it

I'm deploying this Windows Forms application, which is actually a simple, but pretty text editor with some features like Intelissense and syntax highlighting. The application was tested on lots of environments and still, it managed to not work on any of the customer's computers. The culprit was the following innocent line:
MyFont = new Font("Courier New", 10);
Strange as it may seem, it was throwing the following exception:
************** Exception Text **************
System.TypeInitializationException: The type initializer
for "ICSharpCode.TextEditor.Document.FontContainer" threw an exception. --->
System.ArithmeticException: Overflow or underflow in the arithmetic operation.
at System.Drawing.Font.Initialize(FontFamily family, Single emSize,
FontStyle style, GraphicsUnit unit, Byte gdiCharSet, Boolean gdiVerticalFont)
at System.Drawing.Font.Initialize(String familyName, Single emSize,
FontStyle style, GraphicsUnit unit)
at System.Drawing.Font..ctor(String familyName, Single emSize)

After a lot of Googling I found this KB article which mentions a similar problem and a possible solution. It was pretty close to the solution, but it didn't work. It seems to be some other software or driver interfering with my .NET application. I blame it on their anti-virus, as we tested it on lots of different hardware and software configurations (notebooks, desktops, developer machines) and none of them worked, but the same corporate anti-virus was present at all of these machines.
Since I cannot tell them to disable their anti-virus, lots and lots of desperate Googling (and the customer in need of the application), what I ended up doing was:
DefaultFont = new Font("Courier New", 10);
catch (ArithmeticException)
DefaultFont = new Font("Courier New", 10);

where ResetFPU stands for:
[DllImport("msvcr70.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int _controlfp(int n, int mask);

const int _RC_NEAR = 0x00000000;
const int _PC_53 = 0x00010000;
const int _EM_INVALID = 0x00000010;
const int _EM_UNDERFLOW = 0x00000002;
const int _EM_ZERODIVIDE = 0x00000008;
const int _EM_OVERFLOW = 0x00000004;
const int _EM_INEXACT = 0x00000001;
const int _EM_DENORMAL = 0x00080000;
const int _CW_DEFAULT = ( _RC_NEAR + _PC_53 +

static void ResetFPU()
_controlfp(_CW_DEFAULT ,0xfffff);

I hope this helps someone in the future...

You should post your "ResetFPU" (or "_controlfp") function to

Thanks! This really helped me.

The same problem occurs when initilizing a form through a CCW. Using SAX basic from one of our apps to initilize a winform extension to the app. This helped!

Patrik Löwendahl [C# MVP] - 'Elegant code by witty programmers'
There are actually a bunch of FPU control ops on windows. The code shown basically sets the control word (Masks all exceptions, sets rounding to NEAR and precision to 53)

Msdev/Devenv by default always masks exceptions so that nothing is thrown. It could happen that a third party code block explicitly unmasks the exceptions (using _controlfp). They will blow up if they are called "after" the client has already corrupted the fp.

We typically follow any such exception with
- reset the control word. _controlfp()..
- clear the fp exception _clearfp()

Just in case this keeps continuing. Add a cal to _controlfp() in the ResetFPU function.
Has anyone found the ResetFPU function for VB.NET, and not just c#?
Thats awesome!

I had the same problem too, works like a champ on some machines, throws this bizzaire exception on others.

This really helped! Thanks!!
However mean your gold in wow life is,buy wow gold meet it and live it ;wow gold cheap do not shun it and call it hard names.It is not so bad as you are.It looks poorest when you are richest.The fault-finder will find faults in gold kaufen love your life,poor as it is.maple meso You may perhaps have some pleasant,thrilling,maplestory power leveling glorious hourss,even in a poor-house.The setting sun is reflected from the windows of the alms-house as brightly as from the rich man's abode;the snow melts before its door as early in the spring.sell wow gold I do not see but a quiet mind may live as contentedly there,cheap mesos and have as cheering thoughts,as in a palace.The town's poor seem to me often to live the most independent lives of any.May be they are simply great enough to billig wow gold receive without misgiving.Most think that they are above being supported by the town;powerlevel but it often happens that buy maplestory mesos they are not above supporting themselves by dishonest means.which should be more disreputable.Cultivate poverty like a garden herb,like sage.Do not trouble wow powerleveln yourself much to get new things,maple mesos whether clothes or friends
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?