Are TweenObjects automatically GC'ed from within a local scope?

Alright, so I’m not 100% sure about this which is why I came here.

Here’s an example of what I mean:

local function Run_Tween()
local tween_object = tween_service:Create(workspace.Baseplate, TweenInfo.new(1, Enum.EasingStyle.Linear), {Transparency = 1})

tween_object:Play()

tween_object.Completed:Connect(function()
print'blah'
end
end

I’m referring to the .Completed event connection object. I would assume since the tween object itself is not referenced anywhere else in the code, that it would be GC’ed when this function pops off the stack. However, I’m not 100% sure regarding this so I just wanted to confirm my thinking.

Thanks :slight_smile:

Since you’re making a connection, your tween object is still alive. Thus you have a memory leak.

I had some code like this which I posted in a discord server:

local function fast_spawn(f, ...)
    local bindable_event = Instance.new("BindableEvent")
    bindable_event.Event:Connect(f)
    bindable_event:Fire(...)
end

And I was told that this would leak since the connection kept the bindable alive. I had to either disconnect the connection or just destroy the bindable.

For your case you might as well disconnect the connection.

1 Like

I guess that makes sense.

Cheers!