Task.delay / stack overflow

Will such function cause stack overflow ?

local function recurse(count)
	count += 1
	task.delay(0, recurse, count)
end
task.delay(0, recurse, 0)
1 Like

It wouldn’t, there is small delay even when the delay is set to 0.

I’ve seen this :

I wonder if using task.delay may cause to hit that recursion limit

Oh it sure will, you are making a reference to reference and so on

It does not, it’s the same as this:

local count = 1
while task.wait() do
    count += 1
end

Just that, this yields while your code does not.

Okay I have tested those 2 methods :

-- Method without delay
local function recurse(count)
	count += 1
	if count >= 200 then error("test") end
	recurse(count)
end
recurse(0)

-- Method with delay
local function recurse(count)
	count += 1
	if count >= 200 then error("test") end
	task.delay(0,recurse,count)
end
task.delay(0,recurse,0)

And in the first case, the stacktrace actually shows (x199)
while in the 2nd case it doesn’t.
So I’ll conclude that you are right

A stack overflow is a type of buffer overflow error that occurs when a computer program tries to use more memory space in the call stack than has been allocated to that stack.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.