I’m wondering if this would cause a memory leak, and if there’s anything I can do to delete the functions when the function “destroyModel” is called!
Here is my ModuleScript code:
local vmStorage = game:GetService('ReplicatedStorage'):WaitForChild("Viewmodels")
local module = {["Signals"]={ }};
function module:newVM()
local maid = {};
local viewmodel = vmStorage:WaitForChild("TempVM"):Clone();
function maid:destoryModel()
if viewmodel then
viewmodel:Destroy()
return
end
end
return maid
end
return module
I don’t know does it cause a memory leak, but I usually just set the local that holds the module to nil with the mindset of “There’s no way to run nor access this function anymore, garbage collector should be smart enough to just remove this”.
basically,
local a = require(module):newVM()
a:destroyModel()
a = nil
Just add viewmodel = nil after the “:Destroy()” instance method is called on it.
function maid:destoryModel()
if viewmodel then
viewmodel:Destroy()
viewmodel = nil
return
end
end
This way the userdata which the variable “viewmodel” represents will be correctly collected by the garbage collector upon its direct next garbage collection cycle.