简体   繁体   中英

Can I prevent StackOverflowException from crashing my debuggee in Visual Studio?

I'm debugging a 3rd party library that is littered with properties that occasionally throw StackOverFlowException.

Whenever I look at objects from this library in the Watch window, I end up getting the error message "Function evaluation was aborted" and the app I'm debugging crashes.

I wrote an example of a property that reproduces this (when trying to evaluate it in the Watch window):

    private static int CausesStackoverflow
    {
        get { return CausesStackoverflow; }
    }

Is there any way to evaluate properties in the Watch window without risking my app crashing due to a Stackoverflow?

You should prevent (using counters or other tricks) and not catch StackOverflowExceptions.

Since this is 3rd party code (so I suppose it can't be changed) you can try this:


Starting with 2.0 a StackOverflow Exception can only be caught in the following circumstances .

  1. The CLR is being run in a hosted environment where the host specifically allows for StackOverflow exceptions to be handled
  2. The stackoverflow exception is thrown by user code and not due to an actual stack overflow situation (Reference)

From MSDN StackOverflowException page :

In prior versions of the .NET Framework, your application could catch a StackOverflowException object (for example, to recover from unbounded recursion). However, that practice is currently discouraged because significant additional code is required to reliably catch a stack overflow exception and continue program execution.

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. For example, if your application depends on recursion, use a counter or a state condition to terminate the recursive loop. Note that an application that hosts the common language runtime (CLR) can specify that the CLR unload the application domain where the stack overflow exception occurs and let the corresponding process continue. For more information, see ICLRPolicyManager Interface and Hosting the Common Language Runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM