I’m not exactly sure what a stack overflow exception is. I got this error when I called a method in another form.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Sometimes programs misbehave. Programmers write their programs by implementing some logic and then testing that it works correctly. But if the programmer makes a mistake in the logic, bad things can happen.
Probably one of the most common ways that a program can go awry is with an infinite loop. Take this sample program that tries to add up the first n integers:
What will happen? Where’s the bug? We forgot to increment
iand so it will never exceedn. When you just run this it runs and hangs. It will never stop on its own.But if you have an infinite loop in a program and you use resources in that loop, you could run out of resources. Let’s take another program that uses recursion to find the sum:
This method is also buggy. It too has an “infinite loop” because we forgot to include the termination condition. What happens when we run it?
In C# every time you call a method you use up a little of a resource called a stack. The stack allows the program to keep track of who called who so that when methods return, it returns to the right place. Method arguments and local variables also use up stack space. When methods end and return, they free up the stack space that they used.
The problem is that the stack uses memory and so it is not an infinite resource like time!
So there are two primary ways that a program can produce a
StackOverflowException:The first problem is demonstrated by the second method above. But even if we fix that method to add a termination condition, while it will work for smaller numbers it will eventually fail before producing a sum for sufficiently large values of n.
Most garden variety
StackOverflowExceptions are of the first type but if you don’t use recursion carefully in your methods you can run into the second problem as well.When debugging a
StackOverflowExceptionthe call stack will often look like this:or sometimes
AcallsBwhich callsCand then callsAagain, etc. You just have to find the loop, set a breakpoint in in, a diagnose what is going on.