如何调试在.NET中stackoverflowexceptionNET、stackoverflowexception

2023-09-03 04:45:14 作者:顾柒

方案:

我刚刚上过辊和实施了一堆code,但是当我执行它我抛出一个StackOverflowException?该StackOverflowException没有一个堆栈跟踪,所以我卡住了。我知道为什么堆栈溢出可能发生,但要解决这个问题,我需要知道它的根。

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.

所有我得到的是:类型'System.StackOverflowException的未处理的异常发生标记 - 您 - it.dll

All I'm getting is: An unhandled exception of type 'System.StackOverflowException' occurred in tag-you're-it.dll

选项:

扫描通过所有的变化和尝试针尖问题。 (可能是缓慢的) 使用调试器和单步调试,直到你找到问题。 (大概比1更好。) 使用的配置文件和寻找最调用的方法。 ?

PS:

这是一个假设情况(尽管不太常见),因此没有code是可用的。

This is a hypothetical situation (although not too uncommon) and therefore no code is available.

推荐答案

这是几乎总是由于递归。无论哪种方法自称,或方法调用,调用回等的方法。

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.

要找到它:

更新时间::我不知道,但显然你不能得到的堆栈跟踪 StackOverflowException (我猜的东西做与不能够赶上之一,要么)。但是有办法获取转储as这里提到的。 在ReSharper的将表明,称自己的方法(它把一个小绿圈中的侧栏递归调用),但它不会赶上递归当涉及两个或两个以上的方法。 使用像蚂蚁探查的工具,看看哪些方法被调用的次数最多。 请密切留意事件可能调用code火灾同时也意味着相同的事件触发,导致一个循环。 UPDATED: I didn't realise, but apparently you can't get the stack trace for a 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. ReSharper will show methods that call themselves (it puts a little green circle in the sidebar for recursive calls) though it won't catch recursion where two or more methods are involved. Use a tool like ANTS Profiler to see which methods are called the most times. Keep an eye out for events that fire that might call code that means the same event fires again, causing a loop.

有时候,你会得到错别字这样的,太:

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...
}

这是原因之一,我个人现在preFER使用自动属性。

Which is one reason why I personally now prefer to use automatic properties.