Re: System.StackOverflowException

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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.

.



Relevant Pages

  • Re: System.StackOverflowException
    ... > Most stack overflows are caused by infinite recursion loops. ... Deep method call nesting, usually because of deep recursion, but not ... The imprudent use of structs in .NET. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Please Explain where will the struct be stored if it is declared inside the Class
    ... forget about structs for a second. ... can be stored either on the stack, or on the heap. ... First, think about the stack. ... A struct would act exactly the same as any of these decimals and ints. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Difference between nullable class and nullable<> structure
    ... The heap, in general, is open memory, while the stack is program memory. ... Now, anyone who has programmed for a good length of time will note that this statement is not absolutely 100% correct and possibly even misleading, but it is very useful when explaining why a person who writes only structures and no classes is more apt to end up with an out of stack memory condition in his program because he heard that structures are faster. ... For a program using exclusive structs as the user-defined types to have the persistent instances of those structs allocated on the stack, you'd have to make a new method call for each object you want to allocate, and that method would have to not return until you no longer need that object. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PATCH] Fix crash in viafb due to 4k stack overflow
    ... On a new attempt the box survived fbset "1280x1024-60" though ... So it's pretty probable that either it gets near the limit of stack ... For viafb_ioctlI did put all the structs in a single union ... switch { ...
    (Linux-Kernel)
  • Re: Memory (Structure vs Class)
    ... Such a block of memory which is ... actually so can structs ... heap while structures would be the stack unless declared global. ...
    (microsoft.public.vb.general.discussion)