You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If these are not cleared when a promise completes, this can lead to unnecessary prevention of GC. An :andThen or :finally can include callbacks with upvalue references. Since promises keep strong references to these callbacks, the callbacks and their upvalue references will never be collected.
Here's a realistic example:
localgameReadyPromise=getGameReadyPromise() -- this promise exists forever and completes when all modules have loaded-- ...game.Players.PlayerAdded:Connect(function(player)
gameReadyPromise:andThen(function()
print(player.Name, "has joined the game!")
-- load the player into the gameend)
end)
If any players join the game before gameReadyPromise completes then their player objects can never be garbage collected ultimately due to references originating in _queuedResolve.
Here's a simple test:
localfunctiontestGc(callback)
localgcTester= {}
localweakRef=setmetatable({gcTester}, {__mode="v"})
callback(gcTester)
gcTester=nilwait(10)
return#weakRef==0and"GCed" or"Not GCed"endlocalpersistentPromise=Promise.delay(0)
localdidGetGced=testGc(function(upvalueGcTester)
persistentPromise:andThen(function()
localthisUsesUpvalue=upvalueGcTesterend)
persistentPromise:await()
-- the above callback will *never* be called again, so ideally the callback-- and upvalueGcTester would be garbage collected, but they won't be:end)
print("Did upvalueGcTester get collected?:", didGetGced)
This can be solved by clearing out or replacing _queuedResolve, _queuedReject, and _queuedFinally when the promise completes (such as in _finalize).
The text was updated successfully, but these errors were encountered:
roblox-lua-promise/lib/init.lua
Lines 219 to 221 in 9db7364
If these are not cleared when a promise completes, this can lead to unnecessary prevention of GC. An
:andThen
or:finally
can include callbacks with upvalue references. Since promises keep strong references to these callbacks, the callbacks and their upvalue references will never be collected.Here's a realistic example:
If any players join the game before gameReadyPromise completes then their player objects can never be garbage collected ultimately due to references originating in
_queuedResolve
.Here's a simple test:
This can be solved by clearing out or replacing
_queuedResolve
,_queuedReject
, and_queuedFinally
when the promise completes (such as in_finalize
).The text was updated successfully, but these errors were encountered: