Search code examples
c#functionmethodscompiler-errorsstack-overflow

Please define a stack overflow exception -- c#


I'm not exactly sure what a stack overflow exception is. I got this error when I called a method in another form.


Solution

  • Sometimes programs misbehave. Programmers write their programs by implementing some logic and then testing that it works correctly. But if the programmer makes a mistake in the logic, bad things can happen.

    Probably one of the most common ways that a program can go awry is with an infinite loop. Take this sample program that tries to add up the first n integers:

    static int Sum(int n)
    {
        int sum = 0;
        int i = 1;
        while (i <= n)
            sum += i;
        return sum;
    }
    

    What will happen? Where's the bug? We forgot to increment i and so it will never exceed n. When you just run this it runs and hangs. It will never stop on its own.

    But if you have an infinite loop in a program and you use resources in that loop, you could run out of resources. Let's take another program that uses recursion to find the sum:

    static int RecursiveSum(int n)
    {
        return n + RecursiveSum(n - 1);
    }
    

    This method is also buggy. It too has an "infinite loop" because we forgot to include the termination condition. What happens when we run it?

    Process is terminated due to StackOverflowException.

    In C# every time you call a method you use up a little of a resource called a stack. The stack allows the program to keep track of who called who so that when methods return, it returns to the right place. Method arguments and local variables also use up stack space. When methods end and return, they free up the stack space that they used.

    The problem is that the stack uses memory and so it is not an infinite resource like time!

    So there are two primary ways that a program can produce a StackOverflowException:

    1. An infinite loop of methods calling methods calling methods that never return
    2. A series of too many recursive call levels that would end eventually, but the stack runs out first

    The first problem is demonstrated by the second method above. But even if we fix that method to add a termination condition, while it will work for smaller numbers it will eventually fail before producing a sum for sufficiently large values of n.

    Most garden variety StackOverflowExceptions are of the first type but if you don't use recursion carefully in your methods you can run into the second problem as well.

    When debugging a StackOverflowException the call stack will often look like this:

    ...
    Program.RecursiveSum(int n = -7770) Line 26 + 0xf bytes C#
    Program.RecursiveSum(int n = -7769) Line 26 + 0xf bytes C#
    Program.RecursiveSum(int n = -7768) Line 26 + 0xf bytes C#
    Program.RecursiveSum(int n = -7767) Line 26 + 0xf bytes C#
    Program.RecursiveSum(int n = -7766) Line 26 + 0xf bytes C#
    The maximum number of stack frames supported by Visual Studio has been exceeded.    
    

    or sometimes A calls B which calls C and then calls A again, etc. You just have to find the loop, set a breakpoint in in, a diagnose what is going on.