Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StackOverflowException in .NET 4

The following code works fine until I upgrade to .NET 4 (x64)

namespace CrashME
{
    class Program
    {
        private static volatile bool testCrash = false;
        private static void Crash()
        {
            try
            {
            }
            finally
            {
                HttpRuntime.Cache.Insert("xxx", testCrash);
            }

        }

        static void Main(string[] args)
        {
            Crash();
            // Works on .NET 3.5 , crash on .NET 4
        }
    }
}

Did I just uncover a runtime bug, or is there some issue with my usage?

like image 680
Sam Saffron Avatar asked Aug 25 '10 00:08

Sam Saffron


People also ask

What is a stackoverflowexception?

It can overflow. Typically the StackOverflowException is triggered by a recursive method that creates a deep call stack. The problem is linked to the concept of the stack memory region in general. Example. This program defines a method that causes an infinite recursion at runtime. The Recursive () method calls itself at the end of each invocation.

What is a stack overflow exception in C++?

The exception that is thrown when the execution stack exceeds the stack size. This class cannot be inherited. The following example uses a counter to ensure that the number of recursive calls to the Execute method do not exceed a maximum defined by the MAX_RECURSIVE_CALLS constant.

What is the initial value of stackoverflowexception in C++?

StackOverflowException uses the HRESULT COR_E_STACKOVERFLOW, which has the value 0x800703E9. The Localloc intermediate language (IL) instruction throws StackOverflowException. For a list of initial property values for a StackOverflowException object, see the StackOverflowException constructors.

What causes stackoverflowexception in C++?

And After nearly 80,000 invocations, the stack memory space is exhausted and the program terminates. Info Usually, the StackOverflowException is caused by an infinite or uncontrolled recursion. The final numbers printed by the program execution are displayed in the Output section.


2 Answers

This would appear to be a bug in the CLR - you should report it to Microsoft.

Note that the StackOverflowException occurs as the CLR attempts to execute the Crash, not during the execution of the Crash method - the program in fact never enters the method. This would appear to indicate that this is some low-level failure in the CLR. (Also note that the thrown exception also has no stack trace).

This exception is incredibly specific to this situation - changing any one of a number of things fixes this, for example the following code works fine:

private static void Crash()
{
    bool testCrash2 = testCrash;
    try { }
    finally
    {
        HttpRuntime.Cache.Insert("xxx", testCrash2);
    }
}

I would recommend that you report this to Microsoft, but attempt to work around the issue by tweaking your code in the meantime.

like image 91
Justin Avatar answered Oct 06 '22 15:10

Justin


I can reproduce it on an x86 machine. The following code also fails:

        try
        {
        }
        finally
        {
            var foo = new List<object>();
            foo.Add(testCrash);
        }

However, the following code succeeds:

        try
        {
        }
        finally
        {
            var foo = new List<bool>();
            foo.Add(testCrash);
        }

I thought it might have something to do with the boxing of volatile fields within the finally block, but then I tried the following (which also fails):

        try
        {
        }
        finally
        {
            bool[] foo = new bool[1];
            foo[0] = testCrash;
        }

Very interesting problem...

like image 22
Jim Killingsworth Avatar answered Oct 06 '22 14:10

Jim Killingsworth