Skip to content

Commit

Permalink
Fix memory leak due to events with no listeners.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNexusAvenger committed Sep 8, 2021
1 parent ce50332 commit 6a07869
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Event/RobloxEvent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function RobloxEvent:__new()
self:InitializeSuper()
self.Connections = {}
self.BindableEvent = Instance.new("BindableEvent")
self.CurrentWaits = 0

--For deferred events, the arguments need to be stored.
--LastArgumentsStrong will keep the reference around and prevent
Expand Down Expand Up @@ -100,6 +101,10 @@ end
Fires the event.
--]]
function RobloxEvent:Fire(...)
--Ignore if there are no connections.
--If continued, self.LastArgumentsStrong will be populated and never cleared, leading to a memory leak.
if next(self.Connections) == nil and self.CurrentWaits <= 0 then return end

--Store the arguments.
local UUID = HttpService:GenerateGUID()
local Arguments = table.pack(...)
Expand All @@ -116,7 +121,9 @@ is fired. Returns what was fired to the signal.
--]]
function RobloxEvent:Wait()
--Wait for the event.
self.CurrentWaits = self.CurrentWaits + 1
local UUID = self.BindableEvent.Event:Wait()
self.CurrentWaits = self.CurrentWaits - 1

--Return the arguments.
local Arguments = self.LastArguments[UUID]
Expand Down

0 comments on commit 6a07869

Please sign in to comment.