Will this memory leak?

Just a quick question which I kinda had a brainfart on.

If I clone a model (in this case a Player’s Character), and store it in a table, and then Model:Destroy() is called, will the table still hold a reference to this Model, and will it memory leak?

Thanks

1 Like

No, :Destroy() erases all connections to it.

It erases connections, yes, but I’m storing a reference to the model not a connection

I believe:Destroy() just parents to nil at the end? So isn’t the model technically in the game, and therefore there would still be a reference to it and it wouldn’t GC?

Table basically is:

{
    [Player.UserId] = Player.Character:Clone()
}

And then I plan to use the “FakeCharacter” and put it in the DebrisService to clean up if unused. Will a reference still exist, causing a memory leak?

No, :Remove() just parents things to nil.

:Destroy() does destroy connections but it should also remove it from everything else entirely and completely disposes it. It shouldn’t cause a leak.

Wiki Reference: https://developer.roblox.com/en-us/api-reference/function/Instance/Destroy

Yes, the model will remain in memory unless the table is garbage collected or the model is removed from the table.

1 Like

This is incorrect. All that Instance:Destroy does is set the parent of the instance to nil, lock the parent property, disconnect all connections and call Instance:Destroy on all children.

4 Likes

GAH! Looks like I made a fatal mistake, sorry!

Alright, thanks! I guess I’ll just use a delay and remove it myself instead of using DebrisService.

1 Like