Events do not get unbound on Producer deletion

Hey folks!

I’m not sure if this is intended behavior or a bug, but events don’t seem to be unbound whenever the instance they were bound to is deleted. Obviously, the event can’t fire anymore- but it still remains in memory. A minimally reproducible example can be ran below:

local part = Instance.new("Part", workspace)
for i=0,10000000 do
	part.Touched:Connect(function() end)
end
part:Destroy()

This occurs with any RBLX event I’ve tried it on. Looking at the memory usage of the game, even after the part is deleted- all those touched events still remain in memory.

If this is intended, then please educate me on the intended procedure in multi-script games. That is when scripts are created on a per-object basis. If I have a script that attaches an event to its parent, should this script now have to listen to when that parent is destroyed so that it can unbind its event?

Thanks again :slight_smile:

Very strange indeed. I think you could do something like

-- Set variable for part
local part = Instance.new("Part", workspace)

-- Create function that will be run when part is touched
TouchedEvent = function()
     print("Touched")
end

-- Connect the touched event to this function
local event = part.Touched:Connect(TouchedEvent)

-- Disconnect this function from the touched event when the part is being removed
part.Destroying:Once(function()
     event:Disconnect()
)

But I do think this it’s weird for functions to remain in memory like that, it just makes it more complicated to have to manually disconnect events to save memory. I’m not really sure if it’s a bug, or whatever its purpose could be if it is intended.

1 Like

Actually I’m curious, can you check if a :Once() function is also kept in memory?

Just tested, the same behavior with :Once(). Additionally, even if you do disconnect the signals- it still leaves a big memory footprint. Something doesn’t seem to be getting deleted regardless.