basePart:Destroy w/ Dictionary[basePart]={stuff}: Memory Leak?

So I have some special abstract not always on the double conditions of some special parts.
I have a dictionary so if I need a parts specialization, I can throw it in, and it will return {stuff}.

If I destroy the part (and thus the index I suppose), should I make sure I remove the value(s {stuff}) somehow or will Garbadge Collection do it for me?

When you destroy a part it doesn’t hide it from Lua. If you want the part (or any instance really) to still be collected, you’ll need to remove all references to it from the Lua end.

2 Likes

How do I go about doing that?

local part = Instance.new("Part")
local partAsKeyDictionary = {}
partAsKeyDictionary[part] = "do I terminate with my reference"

part:Destroy()
--part = nil does nothing when placed here
for key, value in pairs(partAsKeyDictionary) do
	print(key, value) -- this does print!  and key.Parent; part.Parent: nil
end

I guess I would just have to clear the dictionary with the removing of the part? Seems unprofessional.

(Edit)

Also the learn Roblox wiki whatever you call it says:
Description:

Sets the Instance.Parent property to nil, locks the Instance.Parent property, disconnects all connections and calls Destroy on all children. This function is the correct way to dispose of objects that are no longer required. Disposing of unneeded objects is important, since unnecessary objects and connections in a place use up memory (this is called a memory leak ) which can lead to serious performance issues over time.

And I’m just left wondering if that old local a = part is still keeping the part undead in hyperspace.

Your local part is one reference, and its key status in the partAsKeyDictionary table is another. When you dispose of the part, you can :Destroy() it and then remove it from the dictionary by doing partAsKeyDictionary[part] = nil.

It’s not necessary to dispose of local part in the same way, because this is automatically done for you at the end of the scope. The same can be said of the table, actually, unless you put any references to it outside of the current scope.

1 Like