4

I have a StackOverflowException which is occurring deep inside the LINQ DataContext upon a call to SubmitChanges. After a lot of time wasted trying to pinpoint where the overflow is occurring, I can't seem to figure it out.

How can I see what the stack looked like just before the stack overflow is shown?

Josh M.
  • 26,437
  • 24
  • 119
  • 200

1 Answers1

3

You cannot catch StackOverflowException unless it's thrown by the user code. (more info)

  • In visual studio, from the "Debug" menu, choose "New Breakpoint > Break at Function..."
  • In the "Function" field of the "New Breakpoint" dialog, enter StackOverflowException.StackOverflowException
  • Run your program in the debugger. Once you get the stack overflow, the debugger will stop at your breakpoint.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I'll try it. VS tells me `IntelliSense could not find the specified location.` Is this okay? – Josh M. Mar 08 '12 at 18:13
  • @JoshM. Good point! I always get this warning too, and I ignore it. – Sergey Kalinichenko Mar 08 '12 at 18:20
  • This didn't work for me, VS still runs until the SO is thrown and upon trying to view the stack, I get `cannot evaluate expression because the current thread is in a stack overflow state.` – Josh M. Mar 08 '12 at 18:43
  • @JoshM. I just tried it on my computer, and [it looks like we're out of luck on this one](http://stackoverflow.com/questions/1599219/c-sharp-catch-a-stack-overflow-exception): I was able to catch only the SOE that I threw myself, not the real ones :( – Sergey Kalinichenko Mar 08 '12 at 19:01
  • There must be a way to "record" the stack. E.g. write it to a file and then that file would contain the "last stack" before the SO occurred... – Josh M. Mar 08 '12 at 20:46
  • @JoshM. You would need to be able to catch it one way or the other in order to do that. According to Microsoft, starting with .NET 2.0 it is no longer possible See [this answer](http://stackoverflow.com/a/1599236/335858) for a quote from MS documentation and a "recommendation" on how to deal with it (if "you need to count your own stack frames" suddenly passes for a "recommendation"). – Sergey Kalinichenko Mar 08 '12 at 20:54