简体   繁体   中英

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.

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? 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 . This is related to how to break at my code and not how to catch a StackOverflow Exception.

Normally option inside Debugging->General->Enable Just My Code should do the trick. 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.

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.

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.

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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