Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StackOverflowException even after increasing the stack size

I have a C# WinForm application which I am running with x86 mode. It works great with x86 mode. the problem occurs when I run this application with Any CPU mode. I get the below mentioned error:

An unhandled exception of type 'System.StackOverflowException' occurred in XXXXXX.dll

I know this could be caused by infinite loops and what not but the same error should occur in the x86 mode in that case. I know this is not because of infinite iterations. It has something to do with stack overflow.

After doing some research I increased the stack size with Editbin

from

Editbin.exe /Stack:14000000 "$(TargetDir)MyProject.exe"

to

Editbin.exe /Stack:14000000 "$(TargetDir)MyProject.exe"

Any idea what could be the cause and what directions should I go in to?

like image 601
Amrit Singh Dorka Avatar asked Mar 18 '13 20:03

Amrit Singh Dorka


4 Answers

My bathtub is overflowing.

Try getting a bigger bathtub.

OK, I did that. My bathtub is still overflowing.

Try getting an even bigger bathtub.

OK, it's still overflowing! I have a huge bathtub but it is still overflowing!


Getting a bigger bathtub doesn't help if the problem is that the drain is plugged and the faucet is on.

Your problem is likely that your program is attempting to consume an infinite amount of stack. Making the stack bigger won't help.

I know this could be because of infinite loops and what not but the same error should occur in the x86 mode in that case.

This statement is false. There is no requirement that the same error occur.

Why is it different depending on whether or not I'm targetting a particular cpu?

The jitter can generate different code in different scenarios, and sometimes can generate code that turns infinite recursions into infinite loops. Those won't consume infinite stack, but they will run forever.

Suppose for the sake of argument that there is no unbounded recursion.

Then it is possible that there is a very large recursion. Since 64 bit code can up more stack space than equivalent 32 bit code, large recursions can cause out-of-stack errors to occur earlier.

In that case, making the stack larger is a bad idea. Instead, find the deeply recursive algorithm and make it into an iterative algorithm.

like image 158
Eric Lippert Avatar answered Nov 14 '22 22:11

Eric Lippert


The reason is infinite recursion. Attach a debugger and look at the stack trace when exception occurs.

like image 45
alex Avatar answered Nov 14 '22 21:11

alex


The reason that you are getting a stack overflow is simply because you are using too much of the stack.

The reason that you are only getting it in x64 mode and not x86 mode, is most likely because the programs uses more stack space in x64 mode because references take up more space. That you don't get it in x86 mode is just luck, you simply don't use quite enough stack space for it to run out under those specific circumstances.

The solution is to find out what it is in your program that uses so much stack space. You should look for things like recursive functions that run too many levels deep. A well implemented recursive function would use something like 10 or 100 levels of recursion, but if you are using recursion wrong you could perhaps have 10000000 levels of recursion, which would use a lot of stack space.

like image 44
Guffa Avatar answered Nov 14 '22 20:11

Guffa


Can You view the stack trace?

You can read up this property: Environment.StackTrace.

If the stacktrace exceded a specific threshold that you preset, you can return the function. You can also try to replace some recursive functions with loops. (StackOverflowException object cannot be caught by a try-catch block if .net 2.0 and above)

like image 37
Jordan Avatar answered Nov 14 '22 20:11

Jordan