1
votes

In Visual Studio 2015 for C#, I debug a program. The debugger pauses somewhere and raises System.StackOverflowException:

An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

It means that there is infinite recursion going on.

I want to find out which method is initially called and then leads to the exception, so I turn to the Call Stack window, but there are so many stacks that the out-most stacks have been truncated off the Call Stack window:

...
The maximum number of stack frames supported by Visual Studio has been exceeded.

  • Is there some way to find out the out-most stacks when System.StackOverflowException happens?

  • Is there some way to limit the maximum number of stack frames before System.StackOverflowException is raised, so that all the stack frames can be shown in the Call Stack window?

Thanks.

2
First time I see that second message, always could look into callstack when I faced Stackoverflow exception.Rand Random
Can you not see enough of the stack to see where your call cycle is and then work from there?Chris
use ETW to trace Exception raising, here you see the stack when the Exception get raised. Use WPR and my profile to capture the data and analyze the ETL in WPAmagicandre1981

2 Answers

2
votes

Is there some way to limit the maximum number of stack frames before System.StackOverflowException is raised, so that all the stack frames can be shown in the Call Stack window?

Temporary hack: Make the stack frame smaller until you can see the cause. Then restore the size to default. There is no project setting for this, but this old trick might still work:

EDITBIN.EXE /STACK:<stacksize> file.exe

Is there some way to find out the out-most stacks when System.StackOverflowException happens?

One approach is to use the Trace or Debug class to add logging of all calls to whatever the recursing function function is. Call this function "Cursed()" If there are 10 places in your code that call "Cursed()" add a trace to each one before the call:

Trace.WriteLine("Foo calling Cursed()");
... 
Trace.WriteLine("Bar calling Cursed()");

You can add any variables you want.

You can see how to use Trace and Debug at support.microsoft.com, "How to trace and debug in Visual C#" - https://support.microsoft.com/en-us/help/815788/how-to-trace-and-debug-in-visual-c

If you look at the debug or trace output you then see what function and values triggered the infinite recursion.

Add more logging, asserts, conditional breakpoints to narrow things more.

0
votes

You can run your program with my Runtime Flow tool (30-day trial) and should be able to see all method calls before the StackOverflowException.