Roblox OOP: Dataleak preventage

When you want to get rid of an object (OOP class object), do you need to set functions to nil as well in order to prevent dataleaks?

I’ve heard you only need to do that if it has a strong reference to something. If you don’t know if it has any, it’s a safe practice to set everything into the object to nil.

Can’t you just do self = nil;?

That would set it to nil in the current scope, but in another scope it won’t be nil.

How would you destroy methods then?

You need to let the garbage collector collect it. This usually happens when you “forget” about it, by unassigning all variables that reference it.

However, if a strong reference is active (like an RbxScriptSignal or a table that stores an instance), you might run into issues with garbage collection.

3 Likes

to get rid of an oop class object you would do setmetatable(self, nil)