This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental warning to help find connections that are not disconnected
This commit introduces a new configuration option which aims to help Roact users more easily find components which are not properly disconnecting connections when unmounted and are potentially causing memory leaks because of this reason. A full traversal of the component is performed to find all currently active connections but some keys are excluded to reduce false positives. If a connection is referenced in a component but it's closure does not close over self as an upvalue then it is not a concern that the component still has a reference after being unmounted. This commonly occurs through references to component context or the store. Ideally we would be able to inspect the closure associated with each active connection and warn only if the closure uses self as an upvalue. Internal Roact connections are also ignored for this check, so that connections within EventManager do not cause this warning to be emitted.
- Loading branch information
1 parent
380c3d6
commit 5d0edbf
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
local function getActiveConnections(obj, ignoreKeys, name) | ||
local seen = {} | ||
local results = {} | ||
|
||
local function shouldIgnoreKey(key) | ||
for _, k in pairs(ignoreKeys) do | ||
if k == key then | ||
return true | ||
end | ||
end | ||
return false | ||
end | ||
|
||
local function collectActiveConnections(child, namespace) | ||
if seen[child] then | ||
-- Break out of cyclical tables | ||
return | ||
end | ||
seen[child] = true | ||
|
||
for k, v in pairs(child) do | ||
if not shouldIgnoreKey(k) then | ||
local kStr = tostring(k) | ||
local fullNameKey = namespace == "" and kStr or namespace.. "." ..kStr | ||
if typeof(v) == "table" then | ||
collectActiveConnections(v, fullNameKey) | ||
elseif typeof(v) == "RBXScriptConnection" and v.Connected then | ||
results[fullNameKey] = v | ||
end | ||
end | ||
end | ||
end | ||
|
||
collectActiveConnections(obj, name) | ||
return results | ||
end | ||
|
||
return getActiveConnections |