From 5d748b2eec0b5bb2dea4eced79aba2fd994eb20a Mon Sep 17 00:00:00 2001 From: Adam Comella Date: Fri, 26 Aug 2016 18:44:07 -0700 Subject: [PATCH] Fix memory leak in MessageQueue Summary: The MessageQueue has a _debugInfo object where it stores debug information associated with each callback. The size of this structure is currently unbounded. It looks like the code attempted to restrict _debugInfo to a fixed number of entries but due to a logic bug, it leaked around 30 entries for every 1 entry it cleaned up. This change limits the _debugInfo object to around 30 entries. **Test plan (required)** This change is currently being used in my team's app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/9611 Differential Revision: D3781875 fbshipit-source-id: 58c645c52c3e295fe571b7ca7e0d882169c301ef --- Libraries/Utilities/MessageQueue.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Libraries/Utilities/MessageQueue.js b/Libraries/Utilities/MessageQueue.js index 07d9640b1c9..dbeb4eda19b 100644 --- a/Libraries/Utilities/MessageQueue.js +++ b/Libraries/Utilities/MessageQueue.js @@ -31,6 +31,8 @@ const TO_JS = 0; const TRACE_TAG_REACT_APPS = 1 << 17; +const DEBUG_INFO_LIMIT = 32; + const MethodTypes = keyMirror({ remote: null, remoteAsync: null, @@ -168,10 +170,11 @@ class MessageQueue { __nativeCall(module, method, params, onFail, onSucc) { if (onFail || onSucc) { if (__DEV__) { - // eventually delete old debug info - (this._callbackID > (1 << 5)) && - (this._debugInfo[this._callbackID >> 5] = null); - this._debugInfo[this._callbackID >> 1] = [module, method]; + let callId = this._callbackID >> 1; + this._debugInfo[callId] = [module, method]; + if (callId > DEBUG_INFO_LIMIT) { + delete this._debugInfo[callId - DEBUG_INFO_LIMIT]; + } } onFail && params.push(this._callbackID); this._callbacks[this._callbackID++] = onFail;