Most stack overflows are caused by infinite recursion loops. I would
first (strongly) suspect an error somewhere in your code.
Of course, real stack overflows do happen. They can be caused by the
following things, some of which you can do nothing about, other than
increase the stack size.
1. Deep method call nesting, usually because of deep recursion, but not
always. (By deep method call nesting I mean a method that calls a
method that calls a method... repeat hundreds of times.)
2. Methods with very large amounts of local storage (very large or very
many local variables). Unless you're using structs (see below), it's
awfully difficult to get a stack overflow this way.
3. The imprudent use of structs in .NET. Since structs are copied onto
the stack, creating huge structs and then passing them on method calls,
or declaring them as local variables in recursive methods, you chew up
stack space.
I would first suspect that you have an infinite recursive loop. If not,
then I would suspect that you have a very deeply recursing call. If
not, then I would suspect that you're using structs where you shouldn't
(and I would be very surprised because at this point possible
explanations are becoming more and more unlikely).
Fortunately, it's pretty easy to tell: just look at the call stack that
is dumped out as part of the StackOverflow. It should tell you in which
method it died, which should in turn help you decide which situation
you're facing.
If you do, in the end, have to increase the stack size... well, someone
else will have to jump in on that one.