When to break connections?

Hey, and thanks for reading in advance.

In the interest of preventing memory leaks, do I need to manually break connections created on someone’s Player object (I.E. CharacterAdded or a PropertyChangedSignal)?

Also, let’s say I’ve got a folder with numbervalues in it placed inside my character. If I set up any Changed connections on those, are those connections broken when my character dies and a new one is loaded? Does roblox call Destroy on the character and the player?

Your character model gets destroyed then rebuilt when your character dies so yes, any event connections made on a part will be disconnected automatically when the part in question gets destroyed.

The player and character are not automatically destroyed. They are only parented to nil. However, you can destroy them yourself.

This isn’t necessarily true, your player’s character remains in nil space slightly longer than the 3-frame thing as the PlayerScripts still have weak references to them for cleaning them up.

Also an object isn’t fully purged from memory until all references to it is removed, at which GC sees the table is weak with no references and is like welp, better remove this then.

:Destroy() does not clean these references, it merely sets the metatable’s __mode to weak, disconnects all connections and locks the parent to nil.

local tab = {[workspace.Part1] = "Hello World!"}

workspace.Part1:Destroy()
wait(5)
for i in pairs(tab) do
  print(i.Name) --> Part, still there since it has a reference
end

Wait guys I need this cleared up. When the character dies connections aren’t cleared up?? Well good luck to those people who follow those yt tutorials they probably have memory leaks in there games now.

Anyways when the character removes I use character:Destroy() to make sure the connections are cleared since i’m not sure if they actually get cleared.

Nevermind FilteredDev answered my question above

So… The character is destroyed after few frames? Then why can I set its parent to workspace after a long wait in a function connected to Player.CharacterRemoving? Does having a reference to the character change the behavior or did I misunderstand something. I had a variable as a reference to the character (parameter that was given its value by the CharacterRemoving event).

CharacterRemoving has your Character, so it’s created a weak reference. Therefore, the GC cannot clean it up. As for why the Parent property is unlocked is beyond me and should probably be reported.

Wait, why does CharacterRemoving hold a strong reference? It’s just an argument localized to the function, right?

Player.CharacterRemoving:Connect(function(Character)
-- Code
end)
-- If no other references exist to the character, GC should occur

Or am I incorrect here?

@FilteredDev didn’t say that CharacterRemoving holds a strong reference. If your only reference to the character is a variable, it will be garbagecollected when that variable isn’t used anymore. Variables are weak references.