Home > Software design >  Double.NaN causing System.StackOverflowException in Windows 11 (22621.525)
Double.NaN causing System.StackOverflowException in Windows 11 (22621.525)

Time:10-19

Note: The code works in Windows 11 (22000.1098) and earlier but causes stack overflow exception on Windows 11 (22621.525)

I have a bug that is causing me massive problems in a C# program I'm in charge of. It works well in earlier Windows versions (including earlier Windows 11). It also works in debug builds. But it throws an exception in release build. I have drilled down to the individual functions where the problem appears and it´s weird.

The code is something like

MySettings.MySetting setting = new MySettings.MySetting()
{
    Value = Double.NaN,
    Values = new double[] { Double.NaN },
    Special = "" }
});

but if I change the Double.NaN to a numeric value the code works.

I tried to desimplify it as

MySettings.MySetting setting = new MySettings.MySetting();
set1.Value = Double.NaN;
set1.Values = new double[] { Double.NaN };
set1.Special = "";

Removing Special changes nothing, but if either Value or Values are NaN it throws exception.

The settings class in minimal form is

public class MySetting
{
    public Double Value { get; set; }
    public string Special { get; set; }
    public Double[] Values { get; set; }
    public ValueSpecial[] Specials { get; set; }

    public ValueSpecial AddSpecial(string code, string value)
    {
        ValueSpecial special = new ValueSpecial() { Code = code, Value = value };
        return special;
    }

    public void ForgetSpecial(string code)
    {
    }
    
    public override string ToString()
    {
        return "Not today";
    }
}

ValueSpecial is very simple

public class ValueSpecial
{
    public string Code { get; set; }
    public string Value { get; set; }

    public override string ToString()
    {
        return "dummy";
    }
}

The call stack looks very innocent. Main() -> MainForm() -> InitializeComponent() -> MyControl() so it doesn't appear to be any recursion going on.

What has Microsoft changed in this version of Windows and how do I get around it?

Please note that the same binary works on Windows 11 22000.1098 so don't just focus on the code.

It looks like https://stackoverflow.com/a/25208200/1771388 may give the answer.

CodePudding user response:

Not a fix as such but sort of a workaround. The problem happens in the FPU and may be caused by older programs or libraries. Thy it appeared in Windows 11 (22621.525) but not in earlier versions may have to do with changes of how Windows works.

Anyway, you can get around the problem by forcing a reset of the PFU by adding

try
{
    throw new Exception("Please ignore, resetting FPU");
}
catch {}

before you call the function that causes the exception. If you need to do it from more than once place it may be a good idea to wrap it into a function.

  • Related