Notifications
Clear all

[Closed] Recursive Function Crash

there is a limit for ‘one-chain’ recursion. it doesn’t matter how many times you called a recursive function. but your function crashes if a depth of any recursion reaches some specific number. which means for a function that collects all objects in a scene using recursive method – 1000 objects with 300th-grandchild is ok, but one object with 301st-grandchild is not.

Thank you Klunk and Denis for all your input.

Finally I am happy to know that everything revolves around my ignorance rather than a bug.

However, until I can advance to my next scripting level “Max Nurse” I am staying with the old and loyal friend “while”.

By the way, here is the function that lead me to find this issue/limitation, when I tried to run it on a 500 animation node.

http://forums.cgsociety.org/showthread.php?f=98&t=1248087

also i want to say that a recursive function is not always faster in mxs than flat. i’m not sure that the sample from the mxs help correct. because we don’t know the depth(s) of recursion.

i’m using recursive functions a lot. it works usually for me very well. the function in the sample you pointed to is just algorithmicly wrong. any bad code might crash

Denis, same as you I’ve used recursive functions in many cases over the years and still use them in MaxScript and other languages, but never faced this issue before.

Knowing that there is a limit or recursions for functions is of no help, as for a 400 recursions that would be the last place where I would had looked for.

May I ask other developers if they have a 500 lines of a complex recursive function (MXS) and it crashes Max, would be the number of recursions the first place where they would look for? I suppose the majority of the answers would be no.

Well, now we know the limit is pretty low 380-400, so it is a good place to start with, at least it has some value for me, and it is the first place where I will look for whenever this happens, moreover considering there is no literature I could find about this specific issue and all we get when it happens is a bad crash without any additional information.

I wish some of the MaxScript or Max developers would bring some light on this matter, and see if there is a possible workaround. Avoiding the crash and throwing an exception “Too many recursions” would be very appreciated.

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

you can count the depth (number of recursions) in your function and stop it when the number reaches the limit. but the limit you can get only by practical consideration. hopefully this limit will very close on all machines and max(64) versions.

Oh yes. Now that I have measured the limit I know how to track it. Before it was an unknown value to me.

I am getting consistent values in different Max versions and Kostadin confirmed he is getting the same values so I think we have a solid reference.

Now if this is stack dependent rather recursion dependent, then that limit could shift to a much lower one, so it might take me a while until I feel confident using recursive functions and avoiding the stack overflow in some cases.

After all, we can simply unroll the function into a loop and just use it. At the moment I do not have any reference of real performance to compare with.

The values that i get – Windows 8.1 x64, 8GB RAM. All 3dsMax version are x64.

2009 = 435 recursions
2010 = 385 recursions
2011 = 383 recursions
2012 = 383 recursions
2013 = 412 recursions
2014 = 402 recursions
2015 = 402 recursions

After all, it seems the crash is indeed a bug. I don’t understand though why the differences between different Max versions and systems.

http://forums.cgsociety.org/showpost.php?p=5381463&postcount=3

Page 2 / 2