Notifications
Clear all

[Closed] Is there a recursion limit in maxscript?

For some reason every time I run this script, max crashes…

fn testFunc thevalue =
	(
	if thevalue != 5000 then
		(
		thevalue += 1
		sleep 0.01
		print thevalue
		testFunc thevalue
		)
	else return thevalue
	)
	
testFunc 0

I am using recursion, but there is a stopping condition, and it gets to about 1300 recursions before it bails. Does max have some sort of internal limit to the number of times a function can recurse before it assumes it’s in an infinite loop and crashes max? Any way to up that limit?

  • Neil
17 Replies

Recursion has given me problems as well. It is possible to fool Max into thinking it is in an endless loop- I had it print the Listener to a log and recursion crashes end with “– –”. This didn’t seem to be a memory issue, either (my free heap size was still pretty large when it crashed). Neil’s test confirms the problems… I’d really like an answer to this as well.

MAXScript has a hard-coded (in Max 9 and higher) stackLimit of 2048000 bytes. The return addresses of function calls are put on the stack, so you might be overflowing the stack. If the limit is exceeded, a Runtime error should be displayed, but Max should not crash.

Prior to Max 9, the size was 1024000 and you could increase the stackLimit value. Now it is twice that but you cannot effectively change it.

If something crashes Max, it might be a good idea to submit it as a bug…

1 Reply
(@artofsoulburn)
Joined: 11 months ago

Posts: 0

Well, it does, guess I’ll post it as a bug. Hey Bobo, do you have any idea if this link is still monitored?

http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=5600504&linkID=9241177

It hasn’t been updated since max9.

  • Neil

Easy!


[left]fn testFunc thevalue =[/left]
[left]([/left]
   for i = theValue to 5000 do
[left]   ([/left]
	 sleep 0.01
	 print thevalue
[left]   )[/left]
[left]   return 5000[/left]
)
 
testFunc 0

I expected more from all of you!

On a more serious note. If you run the recursion without anything inside the function does it still crash? Everytime I try to print more than a couple of thousand times I am certain to face lockups. (usually by accidentally forgetting an old ‘debug print’)

What if you throw in a GC light:true every 500 iterations?

Haha. Excellent, Now it doesn’t crash. Of course, it’s also now completely useless, but at least it doesn’t crash

You don’t have to print, you can add any number of things inside there and it’ll crash. Obviously my print example was just an example to simplify my actual script.

Tried that, still crashes.

  • Neil

I am quite sure it is.

So apparently this is a known issue, and is basically a problem that can affect any software written in C++. The only way to avoid it is to not use recursion, so you’ll never blow your stack. So I guess I’ll be busy removing any recursion from my scripts this weekend. Just FYI.

  • Neil
2 Replies
(@drdubosc)
Joined: 11 months ago

Posts: 0

Whaa? Whose C++? If this is right, it requires an adjustment in my perception of the universe.

(@artofsoulburn)
Joined: 11 months ago

Posts: 0

I know. I liked using recursion Just to confirm, I also spoke to a few programmers that know nothing about 3dsmax yesterday, and they confirmed that if your C function recurses too deeply, you can overflow your stack memory, and so they tend to use while/do statements instead to do these sorts of things.

  • Neil

I’ve used max’s dynamic arrays to “recurse” things… it’s a cheat but it works… just make sure you’ve lotsa memory allotted to MaxScript…


fn testFunc thevalue =
	(
	valuearray = #(thevalue)
	for chkValue in valuearray do
		if chkvalue != 5000 then
			(
			append valuearray (chkvalue += 1)
			sleep 0.01
			print chkvalue
			)
		else return chkvalue
	)
	
testFunc 0
1 Reply
(@artofsoulburn)
Joined: 11 months ago

Posts: 0

Yup, that’s the workaround I was told about.

  • Neil
 JHN

Does you functions call lots of maxobjects or are you just recursing strings/ints etc…?
Maybe a simple custom dotnet class can step in and do the work, without the limitations you encounter… and with the way you can build c# classes straight in maxscript you don’t even need a compiled dll… just an idea.

-Johan

1 Reply
(@artofsoulburn)
Joined: 11 months ago

Posts: 0

Both.

  • Neil
 PEN

This is very interesting. Now I havent’ run into this sort of thing when recursing hierarchies in Max rigs so I assume I’m not comming close to the amount of recursive calls that it would take to cause such a problem.

Now I’m starting to wonder what will happen with an old save load animation tool that I have that recurses a custom file format that is turned into a tree of structs.

Page 1 / 2