Log In  


Hi,

I get an out of memory when calling this function (recursive)

when i click a button, i will call select_ball(4,4,3) -> now all objects with the same index (neighbours) wil get index 7
This works perfectly fine, but no matter how many objects will be selected, in the end there is an out of memory exception

balls is a 16x10 multidimensional array.

arrow keys to move the cursor and 'z' to click on a ball

function select_ball(x,y,index)
 	if balls[x][y].index==index then
  		balls[x][y]={index=7}
 		if x>1 and balls[x-1][y].index==index then
 	 		select_ball(x-1,y,index)
 		end
 		if x<width and balls[x+1][y].index==index then
  			select_ball(x+1,y,index)
 		end
 		if y>1 and balls[x][y-1].index==index then
 			select_ball(x,y-1,index)
		end
 		if y<height and balls[x][y+1].index==index then
 			select_ball(x,y+1,index)
		end
	end
end

Cart #37921 | 2017-02-28 | Code ▽ | Embed ▽ | No License



I looked at the rest of your code and it turned out you were using btn(4) and not btnp(4). Using btn(), you were calling your function again for several frames. The problem is that your function creates an infinite recursive loop when you call it on a tile that already has 7 as index. I suggest adding this line to the start of your function:

if index==7 then return end

Also you should still swap the btn(4) to btnp(4), it's just cleaner. :)

The out-of-memory error does happen when your recursive functions recurse a lot (or infinitely).


Thanks, changed it it btnp(4), that did the job!!! Thanks, didn't know about that function :-)

And of-course i need the check for index==7 for when i would click on a ball with index==7



[Please log in to post a comment]