Scenario:
I've just been on a roll and implemented a bunch of code, but when I execute it I get thrown a StackOverflowException? The StackOverflowException doesn't have a stacktrace so I'm stuck. I know why a stack overflow might occur, but to fix it I need to know where it's root is.
All I'm getting is: An unhandled exception of type 'System.StackOverflowException' occurred in tag-you're-it.dll
Options:
PS:
This is a hypothetical situation (although not too uncommon) and therefore no code is available.
Starting with the . NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default. Consequently, users are advised to write their code to detect and prevent a stack overflow.
Applying the HandleProcessCorruptedStateExceptionsAttribute attribute to a method that throws a StackOverflowException has no effect. You still cannot handle the exception from user code.
A StackOverflowException is thrown when the execution stack overflows because it contains too many nested method calls. For example, suppose you have an app as follows: C# Copy. using System; namespace temp { class Program { static void Main(string[] args) { Main(args); // Oops, this recursion won't stop. } } }
WinDbg can get the job done, including even getting a sensible (clr) stack trace. You'll need to get WinDbg unless you've already installed it with Visual Studio or Windows SDK. Note: "WinDbg Preview" with the new GUI has worked fine for me.
I suggest to start your process from WinDbg, but of course you can also attach it to a running process if this suits you better.
Note: Right after starting the process, the CLR is not loaded, and .loadby SOS.dll clr
will fail ("unable to find module 'clr'). You have to wait for the CLR to be loaded. To stop execution once that happens perform:
sxe ld clr
Once the CLR is loaded you'll have to perform the following steps to break on StackOverflowException
(enter in the command window / line):
.loadby SOS.dll clr
(not .loadby sos clr
—this can lead to the extension being loaded twice)!stoponexception -create System.StackOverflowException
g
(continues debugging)trigger the StackOverflowException / wait for it to happen
!clrstack
(will print the stacktrace)Notable sources:
This is almost always due to recursion. Either a method calling itself, or a method calling a method that calls it back and so on.
To find it:
StackOverflowException
(I guess something to do with not being able to catch one, either). However there are ways to get a dump as mentioned here.Occasionally you'll get typos like this, too:
private string name; public string Name { get { return Name; } // Ooops! This is recursive, all because of a typo... }
Which is one reason why I personally now prefer to use automatic properties.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With