This question already has an answer here:
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.
I have a 64bit program I'm running on Win 7 64bit machine, built in Visual Studio 2012.
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();
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.
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).
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).
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) { }
}
Therefore, I have several questions:
Question 1:
Is it possible to catch StackOverflowExceptions ever? How do I do it?
Question 2:
When stack overflow happens inside your dll (other project in same solution), what can make VS 2012 debugger think it's "Unknown module"?
Question 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?
Thanks in advance
You are not allowed to catch a real stack overflow exception, which is why it won't work.
Also see this discussion.
I can't answer your second question, other than to say "the program's really messed-up".
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().
1) Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default. Consequently, users are advised to write their code to detect and prevent a stack overflow. For example, if your application depends on recursion, use a counter or a state condition to terminate the recursive loop. Note that an application that hosts the common language runtime (CLR) can specify that the CLR unload the application domain where the stack overflow exception occurs and let the corresponding process continue. For more information, see ICLRPolicyManager Interface and Hosting Overview. (http://msdn.microsoft.com/en-us/library/system.stackoverflowexception.aspx)