简体   繁体   English

System.Diagnotics.StackTrace()上发生StackOverflowException

[英]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: 但是,有一些值得深思的地方:

  1. Opting for Visual Studio(ASP.NET) Development Server(hereinafter Cassini) in Visual Studio 2012 got no problem, so my IIS settings or something like that is a suspect. 在Visual Studio 2012中选择Visual Studio(ASP.NET)开发服务器(以下称Cassini)没有问题,因此我的IIS设置或类似设置令人怀疑。
  2. The stack, at the time the exception occured, was NOT really deep enough. 发生异常时的堆栈还不够深。
  3. This only happens on debugging. 这仅在调试时发生。 Regardless of the configuration(ie Debug/Release). 不论配置(即调试/发布)。

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: 这是我作为解决方法所做的事情:

  1. I added below to .csproj file to define the current version of IDE. 我在下面添加了.csproj文件,以定义当前版本的IDE。 图片
  2. Defined DEBUG constant 定义的DEBUG常数
  3. 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.

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