views:

266

answers:

6

I dont even know what caused it in my application. What is it? I created a new instance of a class (the class was in another file), but at the first time I call a method it throws a StackOverFlow exception.

The only thing that I think would logically throw a stackoverflow exception would be if someone downvoted Jon Skeet.

But seriously now, what is it? I got around it by creating another class in the same file as the first class and using that to call the methods for me.

+6  A: 

As a general rule, a stack overflow exception is caused by a recursive algorithm where the depth of recursion has exceeded the (typically) fixed stack limit. This is usually a result of a bug in the algorithm, but it may also be caused by the data structure you are applying the algorithm to being too "deep".

Here's a trivial example of a buggy recursion (in no particular PL).

function int length(list l) {
    if (empty(l)) {
        return 0;
    } else {
        return 1 + length(l);  // should be 'return 1 + length(tail(l));
    }
}

Calling length for any non-empty list will give a stack overflow in a typical programming language. But even if you correct the bug, calling the method for a long list is likely to cause a stack overflow.

(The exception is when you use a language ... or more strictly a compiler ... that supports tail recursion optimization.)

Stephen C
The algorithm sets itself to call no more than 100 times, why was this thrown? Note, it was ONLY thrown when inside the other file, from the same file it didnt do anything.
Cyclone
You'd have to show us the code to get an answer that.
Stephen C
Ill have the source on my site soonish, I was fooling around with MDI and the code in question was called when you attempt to add multiple child windows, the For loop inside the main class for the Parent window was set to go less than 100 times at all costs. What should moving the call of the addchild sub to the same file do?
Cyclone
+1  A: 

StackOverFlows Exceptions are exactly what they sound like, the stack overflows. Usually this is because you have a circular dependency in your methods. For instance method A calls B and B calls A. Or it could be a recursive method without a base case.

ssteidl
+2  A: 

Without seeing the code it is impossible to tell why this happened but a StackOverflowException is thrown when a thread overflows its call stack. This most often occurs when a method calls itself recursively without any conditional break thus creating infinite recursion. Since each recursion creates a new stack frame an infinite recursion would theoretically create an infinite number of stack frames, I am sure you can now see why the term "stack overflow" is apt.

Andrew Hare
+3  A: 

A stackoverflow exception is when you exceed the allocated stack size, this generally occurs from recursively calling methods and never leaving, it can also be cause by various obscure method chaining. The issue is you probably have something to the extent of the following in the object.

void MyMethod()
{
    MyMethod();
}

The calls will eat up and never free the stack space used because the calls never end execution and the entry point must remain.

P.S. SO was named for the particular exception in question (this is fundamental and not limited to .NET), it's just a clever name for a developer site.

Quintin Robinson
Lol, I know it was named for that, it is also kindof a pun because the logo is a stack of papers overflowing, i.e. too much work in the inbox and you need help.
Cyclone
+1  A: 

This is usually caused by a recursive call to a function where that recursive call never terminates. You may get this in several ways. One way could be a recursive algorithm without a base case, another common one is creating objects A and B that create one of each other in the their constructors, etc.

i recommend you step through the debugger and find out :)

San Jacinto
+1  A: 

The Stack is where the computer stores a list of the functions that are currently being called, and the variables and parameters used. So if function Main calls function A, and then function A calls function B, and they use variables c, d and e, the stack will contain all of that information. However, the stack is only so big. So if function B then calls function C, which calls function D... etc, ending up with hundreds of nested functions, eventually, the stack will "overflow" - there isn't enough space to store another function call.

As other people have noted, this usually happens with a recursive function (where function B calls function B, which then calls function B...) - eventually, the stack will overflow. You will need to find where that recursive function is being called, and why it isn't breaking out of the recursive loop when it's supposed to.

Of course, the problem may not be that it's a buggy recursive algorithm - it may just be that the number of function calls exceeds the size of the stack. So if your algorithm has the potential to call a recursive function a few hundred times, it may be that.

Smashery