简体   繁体   English

从第三方组件抛出StackOverflowException时如何中断我的源代码?

[英]How to break at my source code when StackOverflowException is thrown from third party component?

My code calls a third party component ( which I don't have source code access to), unfortunately, StackOverflowException was thrown from the third party component, and when I look at the stack trace, it's all third party component calling itself. 我的代码调用了第三方组件(我没有源代码访问权限),不幸的是, StackOverflowException是从第三方组件抛出的,当我查看堆栈跟踪时,所有第三方组件都在调用自身。

I can't even tell which line of my code that calls into the offending third party method, because the stack trace is consisted of nothing but the same third party method calling itself, all over again. 我什至不知道我的代码的哪一行调用了有问题的第三方方法,因为堆栈跟踪只由相同的第三方方法组成,而该第三方方法又一次调用了自身。

How to make the VS2015 debugger to break at my source code when StackOverflowException is thrown from third party component? 从第三方组件抛出StackOverflowException时,如何使VS2015调试器在我的源代码处中断? Is this possible at all? 这有可能吗?

I can always try step by step debugging, but my code is so messy that I prefer to do it at the last resort. 我总是可以尝试逐步调试,但是我的代码太杂乱了,我宁愿在万不得已时去做。

Edit: Note that this is different from the question-- C# catch a stack overflow exception . 编辑:请注意,这与问题不同– C#捕获堆栈溢出异常 This is related to how to break at my code and not how to catch a StackOverflow Exception. 这与如何中断我的代码有关, 而不与如何捕获StackOverflow异常有关。

Normally option inside Debugging->General->Enable Just My Code should do the trick. 通常,在Debugging-> General-> Enable Just My Code中的选项应该可以解决问题。 You should mark it and then debugger should stop inside your code instead of 3rd party library. 您应该对其进行标记,然后调试器应停止在您的代码内部,而不是第三方库。 However you need maybe to fulfil some more conditions like ie delete .pdb file. 但是,您可能需要满足更多条件,例如删除.pdb文件。

Here some more details: Understanding Just My Code 这里有更多详细信息: 了解我的代码

EDIT: 编辑:

Yes, Jay you are right sorry for misleading answer without checking. 是的,周杰伦,对您的误导性答案(未经检查)很抱歉。 I checked it now on VS 2015 and I found interesting side-effect which could help you to find proper place in code. 我现在在VS 2015上进行了检查,发现了有趣的副作用,可以帮助您在代码中找到合适的位置。

After Exception window is shown place in code is marked by gray rectangle. 显示“例外”窗口后,代码中的位置用灰色矩形标记。 It means if all windows with source code containing invocation of hanging component will be opened during debug session it should be possible to see where exception occurs. 这意味着,如果将在调试会话期间打开所有带有包含挂起组件调用的源代码的窗口,则应该可以看到发生异常的位置。

在此处输入图片说明

Seems that VS cannot enter debug mode because of lack of stack, but just before hang correct line is marked. 似乎VS由于缺少堆栈而无法进入调试模式,但是在挂起正确行之前被标记。

Condition: Source file must be open. 条件:必须打开源文件。 So if amount of files containing invocation of this "bad" external code is reasonable maybe this could help. 因此,如果包含调用此“不良”外部代码的文件数量是合理的,则可能会有所帮助。

So after a fairly length discussion (above) - My tuppence worth is that I don't think this is actually possible. 因此,在进行了相当长的讨论之后(上),值得庆幸的是,我认为这实际上是不可能的。

According to MSDN , a StackOverflowException will terminate your process... 根据MSDN ,StackOverflowException将终止您的进程...

Starting with the .NET Framework 2.0, you can't catch a StackOverflowException object with a try/catch block, and the corresponding process is terminated by default.

If your process is terminated, you cannot then point the debugger back to your source code. 如果您的进程终止了,则不能再将调试器指向源代码。 that is because there is no reference. 那是因为没有参考。 A simple example: 一个简单的例子:

class Program 
{ 
   static void Main(string[] args) 
   { 
      Foo(); 
   } 

   static void Foo() 
   { 
      Foo(); 
   } 
}

The above program will go into an infinite loop and throw a Stackoverflow exception. 上面的程序将进入无限循环并引发Stackoverflow异常。 You (should) get a message that your process is terminated, and the debugger will show no stack trace information . 您(应该)收到一条消息,指出您的进程已终止,并且调试器将不显示任何堆栈跟踪信息

That last part is important; 最后一部分很重要; if there is no stack trace information - you (or rather your debugger) cannot walk back up the stack to your code. 如果没有堆栈跟踪信息,则您(或更确切地说,您的调试器)无法将堆栈返回到您的代码。

Building this same example (and making it public) in release mode with no PDB files and referencing it from a second console application mimics the OP's question - calling 3rd party code with no source that throws a SO exception. 在没有PDB文件的发布模式下构建相同的示例(并将其公开),并从第二个控制台应用程序引用它来模仿OP的问题-调用第三方代码,而没有引发SO异常的源。

When I do this, I see in my debugger no information other than System.StackOverflowException was unhandled Message: An unhandled exception of type 'System.StackOverflowException' occurred in SoTest.exe (and this is also the case if I enable 'Just my code') - the program subsequently terminates and I have no pointer as to where the process terminated. 当我执行此操作时,我在调试器中没有看到System.StackOverflowException was unhandled Message: An unhandled exception of type 'System.StackOverflowException' occurred in SoTest.exe (如果启用了“仅我的代码”,情况也是如此) ')-程序随后终止,并且我没有指向进程终止的指针。

I would recommend implementing AOP logging ( I have used postsharp - and last time i checked was free - though this might not be the case anymore) to add logging to all your method boundararies in your inherited code, and then step from where the last line of logging occured to find the rogue call in the 3rd party assembly that is causing a SO exception. 我建议实现AOP日志记录( 我曾经使用过postharp-上次检查是免费的-尽管现在可能不再如此了),以便将日志记录添加到继承代码中的所有方法边界,然后从最后一行开始日志记录的发生是在导致SO异常的第三方程序集中找到流氓调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何捕获从第三方库抛出的XmlException? - How can I catch this XmlException thrown from third party library? 步入第三方源代码 - Stepping Into Third Party Source Code 为什么我的数组初始化代码会引发StackOverflowException? - Why does my array initialization code cause a StackOverflowException to be thrown? 如何防止在我运行 WPF MVVM 应用程序时引发“System.StackOverFlowException”异常? - How to prevent the "System.StackOverFlowException" exception being thrown when i run my WPF MVVM application? 如何在我的应用程序中找到StackOverflowException的源代码 - How to find the source of a StackOverflowException in my application 如何从代码中更改“引发异常时中断”调试器行为 - How can I change “Break When an Exception is Thrown” debugger behaviour from code 我的代码在执行时抛出 stackoverflowexception - My code throws stackoverflowexception when i execute 为什么Visual Studio会中断第三方库中引发和处理的异常? - Why does Visual Studio break on Exceptions thrown and handled in a third party library? 如何捕获第三方库中未等待的任务引发的异常? - How to catch exception thrown by a task that's not awaited in a third party library? 如何在我的代码中防止StackOverflowException - How can I prevent StackOverflowException in my code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM