Do OOP Tables Delete After the Script is Deleted?

Hi, this is what I mean:

Module Script:

function Module.new()
  local self = {}

  self.Thing = 1
  self.OtherThing= 2

  return setmetatable(self, Module)
end)

Normal Script:

local obj = Module.new()

If the normal script gets destroyed/deleted, would this cause a memory leak, because the obj still exists in the module? Please help me if you can, thanks!

1 Like

Nope, you should be fine. The object doesn’t “exist” within the module. All objects are just functions essentially. When you call module.new() you’re doing nothing more than calling a function the same way you’d do it in any other script. The “local obj” variable stores the value that function returns in your main script. Once the “obj” variable ceases to exist(I.e. the main script gets deleted) it’s gone for good. You have nothing to worry about. :slight_smile:

1 Like

Sometimes I see people do something like
setmetatable(self, nil)
in :Destroy metamethods. Does that actually affect any performance or anything?

That deconstructs the metatable attached to self. This ensures GC cleans up the metatable. I wrote a thread handler object that uses that method of deconstruction and can confirm it works.