[英]A StackOverflowException occurs on System.Diagnotics.StackTrace()
I've been writing a web application targeted to .NET framework v3.5 with Visual Studio 2013. 我一直在使用Visual Studio 2013编写针对.NET Framework v3.5的Web应用程序。
Indirect recursion in it someties cause a StackOverflowException so I wrote a method which checks if the stack overflows. 间接递归会导致StackOverflowException,因此我编写了一个方法来检查堆栈是否溢出。
public static void CheckStackOverflow() {
StackTrace stackTrace = new StackTrace();
StackDepth = stackTrace.GetFrames().Length;
if(StackDepth > MAXIMUM_STACK_DEPTH) {
throw new StackOverflowException("StackOverflow detected.");
}
}
The problem is that a StackOverflowException occurs at the first line, ie new StackTrace()
, so I cannot take care of it. 问题是,第一行即发生了StackOverflowException,即new StackTrace()
,所以我无法处理它。
I know that calling to StackTrace() also deepens the stack by a couple of levels, so I understand this can happen. 我知道调用StackTrace()还会使堆栈加深两个级别,因此我知道这可能发生。 However, there is some food for thought: 但是,有一些值得深思的地方:
Edit: I tried to changed IIS Express settings and it made no differences. 编辑:我试图更改IIS Express设置 ,并且没有区别。 Also, Trying Local IIS option got no luck, either. 另外,尝试本地IIS选项也没有成功。 So, 所以,
if(RunningWithVisualStudio) { // Start Debugging or Without Debugging
if(UsingCassini) {
throw new StackOrverflowException("A catchable exception."); // expected
} else {
throw new StackOverflowException("I cannot catch this dang exception.");
}
} else { // publish on the identical ApplicationPool.
throw new StackOrverflowException("A catchable exception."); // expected
}
I thought I'd made mistakes configuring IIS Express but now I'm totally lost. 我以为我在配置IIS Express时犯了错误,但是现在我完全迷路了。
Here are what I did as workaround: 这是我作为解决方法所做的事情:
added conditions using preprocessor directives. 使用预处理程序指令添加条件。
public static void CheckStackOverflow() { StackTrace stackTrace = new StackTrace(); StackDepth = stackTrace.GetFrames().Length; int threashold; #if (VISUAL_STUDIO_12 && DEBUG) threshold = MAXIMUM_STACK_DEPTH_FOR_VS12; // set to be a "safe" integer #else threshold = MAXIMUM_STACK_DEPTH; // the one in common use #endif if(StackDepth > threashold) { throw new StackOverflowException("StackOverflow detected."); } }
Where the constnat MAXIMUM_STACK_DEPTH_FOR_VS12 is the manually-found-greatest number that causes no problem. 常量MAXIMUM_STACK_DEPTH_FOR_VS12是手动找到的最大编号,不会引起任何问题。
Now, I can debug and publish the app without changing anything but still love to hear your opinions. 现在,我可以调试和发布该应用程序,而无需进行任何更改,但仍然喜欢听听您的意见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.