C# 无法捕获 StackOverflowException 或查看它发生的位置 [英] C# cannot catch StackOverflowException or see where it happened

查看:24
本文介绍了C# 无法捕获 StackOverflowException 或查看它发生的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这很模糊,但这就是我所能收集到的.我希望有人对这可能是如何发生的有所了解.

I know this is very vague but this is all I could gather. I hope someone has an insight on how this may be happening.

我有一个在 Win 7 64 位机器上运行的 64 位程序,它是在 Visual Studio 2012 中构建的.

I have a 64bit program I'm running on Win 7 64bit machine, built in Visual Studio 2012.

它使用位于另一个项目中的类(编译为 dll).该类有一个有问题的迭代器,可能导致堆栈溢出.调用该错误迭代器的操作是通过任务调用的.

It uses a class that is situated in another project (compiled as dll). That class has a buggy iterator that can cause a stack overflow. The operation that calls that buggy iterator is called through a task.

所以在我的程序中,代码如下所示:

So in my program, code looks like this:

new Task(()=>{
try
{
    DoWork(); //<-- buggy iterator is called inside
}
catch (Exception ex)
{
    //Exception reported to user
}
}).Start();

代码每次都在相同的数据上执行,并且不涉及随机变量或 guid 生成,因此每次都应该产生相同的输出.

The code is executed on same data every time, and has no random variables or guid generation involved so should produce same output every time.

大多数情况下,它会因 StackOverflowException 发生在 Unknown Module 而崩溃(尽管我知道它究竟发生在哪里).如果发生这种情况,我将无法查看发生溢出的代码点,并且 try-catch 块什么也不做!我无法继续执行该程序(如果我在没有调试的情况下运行它,它会显示 必须关闭程序,让我们在线检查解决方案" 系统窗口).

Most of the time, it crashes with StackOverflowException occuring in Unknown Module (even though I know for fact where it happens exactly). If that happens, I cannot view the point in code where the overflow occured, and try-catch block does nothing! I cannot continue executing the program (if I run it w/o debugging, it shows the "Program had to be closed lets check for solution online" system window).

现在,有时它会捕获异常(显示了导致异常的代码中的正确位置).这是随机的,但如果我更频繁地中断任务(例如在任务工作期间随机插入断点),似乎会发生更多.

Now, sometimes it catches the exception (the correct place in code that caused the exception is showed). This is random but seems to occur more if I break the task more often (like randomly insert breakpoints into the task during its work).

但是,我仍然无法继续申请.即使异常发生在应该捕获 StackOverflowException 的 try-catch 块内(它应该捕获它,因为 StackOverflowException 包含在异常中 - C# 不会让您同时捕获两者并明确告诉您 SOE 已包含在 E 中).

However, I still cannot continue the application. Even though the exception happens inside a try-catch block that is supposed to catch StackOverflowException (it is supposed to catch it because StackOverflowException is included in Exception - C# wont let you catch both and tells you explicitly that SOE is already included in E).

我尝试制作一个小程序,它在 try-catch 块内故意造成堆栈溢出,并且它还显示 程序必须关闭,让我们在线检查解决方案" 系统窗口为我而不是正确处理.代码如下:

I tried making a small program that makes deliberate stack overflow inside a try-catch block and it also showed the "Program had to be closed lets check for solution online" system window for me instead of handling that correctly. Here's the code:

    private bool Overflow()
    {
        return Overflow();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        try { Overflow();}
        catch (StackOverflowException ex) { }
    }

因此,我有几个问题:

问题 1:

是否有可能捕获 StackOverflowExceptions?我该怎么做?

Is it possible to catch StackOverflowExceptions ever? How do I do it?

问题 2:

当堆栈溢出发生在您的 dll(同一解决方案中的其他项目)中时,什么会使 VS 2012 调试器认为它是未知模块"?

When stack overflow happens inside your dll (other project in same solution), what can make VS 2012 debugger think it's "Unknown module"?

问题 3:

为什么用部落乐器进行一些奇怪的魔术跳舞会减少这种情况?(我的意思是随机破坏正在进行的程序)

Why doing some wierd magic dancing with tribal musical instruments makes it do that less often? (by that I mean randomly breaking program in-progress)

总的来说,发生了什么?

Just overall, what is going on?

提前致谢

推荐答案

您不允许捕获真正的堆栈溢出异常,这就是它不起作用的原因.

You are not allowed to catch a real stack overflow exception, which is why it won't work.

另见这个讨论.

我无法回答你的第二个问题,只能说程序真的一团糟".

I can't answer your second question, other than to say "the program's really messed-up".

对于您的第三个问题:因为它在单独的线程中运行,所以它运行的代码量将根据您中断程序的时间而有所不同.我认为您能够深入了解这一问题的唯一方法是通过一些繁琐的逐行调试,或大量的 Debug.WriteLine().

For your third question: Because it's running in a separate thread the amount of code that it has run will vary depending on when you break the program. I think the only way you'll be able to get to the bottom of this is through some tedious line-by-line debugging, or lots of Debug.WriteLine().

这篇关于C# 无法捕获 StackOverflowException 或查看它发生的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆