Soo, i’ve run this module about 100k times, and i found that in some runs memory grew about 1.5 GB, in others about 2-3 MB, i had to rewrite it many times, but i don’t know how i can prevent memory leaks, i’m scared that one of those tables is the cause but i don’t know how i can check that
local System = script.Parent
local ISOS = require(game:GetService("ReplicatedStorage").ISOS)
local Event = ISOS.Class({})
function Event:__init()
self.Callbacks = {}
end
-- Connect bindable event (uncompatible with custom)
function Event:Bind(BindableEvent: RBXScriptSignal, Callback, ...)
local Callbacks = self.Callbacks
Callbacks[Callback] = BindableEvent:Connect(Callback, ...)
return Callbacks[Callback]
end
-- Connect custom listener (uncompatible with bindables)
function Event:Connect(Callback: (any) -> any)
local Callbacks = self.Callbacks
if typeof(Callbacks[Callback]) == "RBXScriptConnection" then Callbacks[Callback]:Disconnect() end
if typeof(Callbacks[Callback]) == "thread" then task.cancel(Callbacks[Callback]) end
Callbacks[Callback] = true
local connection = {}
function connection:Disconnect()
if typeof(Callbacks[Callback]) == "thread" then task.cancel(Callbacks[Callback]) end
Callbacks[Callback] = nil
connection.Disconnect = nil
end
return connection
end
-- Fire custom listener
function Event:Fire(...)
local Callbacks = self.Callbacks
for Callback in Callbacks do
Callbacks[Callback] = task.spawn(Callback, ...)
end
end
return Event
Also i have question: How do i’m supposed to detect memory leaks? for now i simply run code a lot of times with and without disconnect function to see if it works