Unhandled Exception: System.StackOverflowException when using Switch Statement?
There is an issue where the switch statement does not work anymore and then Unity complains that another script is to blame for this exception. I don't know why or how this error is occurring but it is stopping me from playing the build of the game inside the editor. The full error reads:
Unhandled Exception: System.StackOverflowException: The requested operation caused a stack overflow.
at Mono.CSharp.ExplicitBlock.EmitMeta (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
The script that has the switch statement with the error has only this inside:
switch(serialNumber)
{
case 0: break;
}
While the other script has thousands of cases (too many to put in this question), why is it complaining?
My speculation from what I am getting from this exception is that a switch statement can hold only so many cases, but if that was true, then how many cases can I put in a switch statement?
I am so confused on this, any help would be appreciated.
Update: When I comment out the line "case 0: break;" from the switch statement. I get this message:
Assets/Scripts/Database.cs(122,17): warning CS1522: Empty switch block
Unhandled Exception: System.StackOverflowException: The requested operation caused a stack overflow.
at Mono.CSharp.ExplicitBlock.EmitMeta (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
Normally that would be a warning, but it shows it as an error and still prevents me from running the build in the editor.
I don't think it is related to the switch. In some cases, the error shows the line where the program broke but it may not be the reason of the exception. $$anonymous$$aybe you got this in a loop and it happens to be the moment when the stack runs out. There is no limit to a switch (at least I have never heard of any) except for common sense and maintainability.
So loops have limits in the stack but switches do not? Then how would I know if I have reached the limit of the loop or the edge of the stack before it has overflown? Is there something I could do or code I can write to prevent this exception?
Comment out the whole switch(serialNumber)
thing - all 4 of those lines. What happens then?
And, thousands of items in a case statement... why?
Ok, after I commented them out, I still get the same error. And the reason I have thousands of cases is for a side project I am working on. There are about 4000-6000 items (cards, in this case) and they each have a different effect depending on how they are played. This Database script holds all the information of those cards and sets them by calling a function within each separate case.
$$anonymous$$ake an array of delegates and use the case value as an index into the array. Or a dictionary that can be indexed by the value or a string or something.
To answer your question to fafase, no, a loop doesn't have limits. What he was saying is that you're somehow calling a function that ends up calling back to the case statement. It ends up "looping" because it keeps recursively calling into the same functions somewhere. That inevitably leads to a stack overflow.
While the other script has thousands of cases (too many to put in this question), why is it complaining?
If there are more then 4 cases, start considering not to use switch-case. if there are more then 10 there will nearly always be a better solution. $$anonymous$$ore then 100 is crazy but thousand*s*. but i don't think the error comes from that.
make sure there is no endless recursion. go backwards, whats the last thing you implemented?
ok, I will start there, and work backwards, maybe you are right if there is endless recursion. It is a start at least.
To paraphrase the other answers: Search for "stack overflow exception." It's a general program$$anonymous$$g problem, so non-Unity sites are best to read about it. You'll get an explanation of what a stack is and what things make it overflow.
Answer by AmasterAmaster · Dec 14, 2015 at 09:32 PM
Well, I think I found the Problem. There is a maximum number of cases you can have in a single switch statement. Any more than the maximum and it will throw this exception. I did some experimenting by taking the switch statement cases out chuck by chunk. Then slowly added the chunks back in (since I did this alphabetically using the strings within each function in each separate case, I did it by letter). Once I reached the offending category letter, I then took the chunk of cases out and only put half in, I repeated the steps until I found the single offending case. But there was nothing different or odd about it (nothing was calling a function recursively).
So I commented the line out and the build works, then un-commented the line and it gives the error again. So to verify that it was not just a fluke, I tested this with a known good case right above it. Commented out that case and un-commented the offending case, and it strangely worked with that case. So I concluded that there might be a maximum of about 5000 case statements for any single given switch statement.
I do appreciate all the help from everyone that suggested to do research and backtracking on this problem. And I should have tried other methods of storing information for items more efficiently (I need to look up array and dictionary searches for this).
(Also if anyone can double check if this is effecting anyone else, then I can make this as an answer, otherwise, if there is an alternative, let me know).
Your answer
Follow this Question
Related Questions
File.Move won't work : IOException: Win32 IO returned ERROR_ALREADY_EXISTS. Path: 0 Answers
Error : Failed to build apk -1 Answers
ComponentChunkIterator is not liking me.. 0 Answers
Missing the class attribute 'ExtensionOfNativeClass' 4 Answers
InvalidOperationException: Collection was modified; enumeration operation may not execute. 0 Answers