How to debug SystemStackError

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
This is a common problem: you've got an infinite recursive loop going on somewhere.

Question is, how to deal with it as a developer. It's not always immediately obvious why it occurs.

After a few minutes of searching I came across this question

http://stackoverflow.com/questions/11544460/how-to-get-a-backtrace-from-a-systemstackerror-stack-level-too-deep

And one of the answers was to write something like

raise "crash me" if caller.length > 500caller is defined in Kernel, btw.This is very easy to do when you're debugging your scripts: figure out where the recursion is occurring, and then insert the line right before it.

For example, given the following code

def test1 test2enddef test2 test1endNow call `test1` or `test2` somewhere in your script.It is trivial, but you know there's a recursive loop and you're going to blow the stack eventually.

If you're printing the stacktrace to console using a script like cidiomar's full error backtrace, it will tell you which line basically blew the stack (I think), but it doesn't tell you anything else.



Ok so now we know line 2 was being visited frequently, so we use the trick above like this

def test1 raise "crash me" if caller.length > 20 test2 # this line was being called recursively repeatedly at some point # so let's interrupt it before ruby complainsenddef test2 test1endI set it to 20 because you shouldn't be calling test2 that many times anyways.Now when you run it, you'll see this in the console:



Now, whether you have any programming experience or not, you can look at the stacktrace and see that there's some repetition going on, and get a better idea where the problem is.

In my case, you can see that the two methods are just calling each other infinitely.

What kind of techniques do you use when it comes to SystemStackError debugging?
 
Last edited by a moderator:

Zeriab

Huggins!
Veteran
Joined
Mar 20, 2012
Messages
1,268
Reaction score
1,422
First Language
English
Primarily Uses
RMXP
I haven't really formalized any particular strategy against infinite loops. I find them generally easy to solve as they usually are easy to reproduce and have a distinct flavor/smell to them.

Look at the stack trace is a good idea as it can contain valuable information.

For RMXP I typically just wrap a begin-rescue block around the $scene.main call in the Main loop.

  while $scene != nil    begin      $scene.main    rescue StandardError => err      printErrorMessage(err) # With backtrace and all      raise if $DEBUG    end  end
More annoying are the stack overflows not triggered by an infinite recursion. The stack btw. is used for your position in the program. When you call a method it puts the method on the stack and runs it. Once the method has been executed it pops off the method and continues from the previous method. (It probably does something more advanced, but you get the gist)

This stack has a limited size and each method in the call chain takes up some space. Should you exceed the limit, well you get a stack overflow error. To me it seems that the limit is very small in RMXP at least.

Recursive algorithms can sometimes be used to hide complexity in the call chain. You could for example imagine a pathfinding or tree searching algorithm you consider your current node and recursively considers other nodes. In this case the length of the path may differ depending on the situation, which as you can guess sometimes may lead to a stack overflow exception and other times not.

A solution could be to rewrite the algorithm to an iterative variant.

I guess you could also without any recursion hit the limit. I haven't encounter it myself, but I bet it would truly be annoying to fix.

*hugs*
 

jamespedid

Veteran
Veteran
Joined
Jun 10, 2013
Messages
30
Reaction score
3
First Language
English

GaryCXJk

Veteran
Veteran
Joined
Dec 24, 2012
Messages
88
Reaction score
46
First Language
Dutch
Primarily Uses
I use basically the same thing. Perhaps a bit differently though, and from a different source.


https://gist.github.com/jbgo/4493822


Anyway, I myself did a test once, and the stack was pretty high.


By the way, it's one of the things people blame on using aliases, while in reality stack overflows aren't caused by that. In most cases, even with fifty scripts aliasing the same method, there shouldn't be any problems for the first ten or twenty iterations.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
Giraki, please do not hijack other people's threads with your own unrelated questions. Please start a NEW thread in RGSSx Script Support asking for help - and provide more information - nobody can help you if they can't see what you've done.


Hiding off-topic posts.
 

Giraki

Warper
Member
Joined
Apr 13, 2015
Messages
3
Reaction score
0
First Language
English
Primarily Uses
Sorry sir, I thought It was a system stack, Ill go start a new thread.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
System Stack errors actually SAY System Stack Error, as shown in the OP's screenshots. However, even if it was, this is not a thread for getting help finding your particular error. It is introducing a way (and asking for other ways in use) to help track one down when you have one.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,862
Messages
1,017,049
Members
137,569
Latest member
Shtelsky
Top