Question about connections and garbage collection

Heya, I have this block of code.

local render_connection = RUN_SERVICE.PreRender:Connect(function(delta_time)
	-- do stuff
end)

overhead_billboard.Destroying:Once(function()
	render_connection:Disconnect()
end)

Will the engine garbage-collect render_connection after it’s disconnected? I have no further references to it, and setting it to nil makes the strict type solver mad, saying that Type 'nil' could not be converted into 'RBXScriptConnection', which is understandable since it doesn’t know it won’t be used later on.

Is it okay to leave it like this?
Thanks for reading :slight_smile:

It will not be collected because there is still a variable you could access it through, but if you ever replace it by writing to the same variable, it can now be collected. Don’t listen to the linter when it complains about types, its just wrong.

1 Like

it’ll be garbage collected when render_connection goes out of scope, because that’s when the upvalue referencing it is no longer accessible and marked for garbage collection.

it’ll also be garbage collected when the variable is reassigned, like azqjanna (no ping) said, because that’ll also cause the previous value to be inaccessible, and marked for cleaning.

2 Likes

Yes, it’ll garbage collect when it’s no longer being referenced or the reference is no longer accessible.

1 Like

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