From ab5b3792752385c7dcb5ad7ecd6bd9f0aa589f01 Mon Sep 17 00:00:00 2001 From: Andrew Clark Date: Mon, 3 May 2021 13:34:19 -0500 Subject: [PATCH] Re-land "Clean up host pointers in level 2 of clean-up flag (#21112)" This re-lands commit 8ed0c85bf174ce6e501be62d9ccec1889bbdbce1. --- .../src/ReactFiberCommitWork.new.js | 21 +++++++++++-------- .../src/ReactFiberCommitWork.old.js | 21 +++++++++++-------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/packages/react-reconciler/src/ReactFiberCommitWork.new.js b/packages/react-reconciler/src/ReactFiberCommitWork.new.js index bbd6f56925225..46f19294562c1 100644 --- a/packages/react-reconciler/src/ReactFiberCommitWork.new.js +++ b/packages/react-reconciler/src/ReactFiberCommitWork.new.js @@ -1382,6 +1382,18 @@ function detachFiberAfterEffects(fiber: Fiber) { fiber.deletions = null; fiber.sibling = null; + // The `stateNode` is cyclical because on host nodes it points to the host + // tree, which has its own pointers to children, parents, and siblings. + // The other host nodes also point back to fibers, so we should detach that + // one, too. + if (fiber.tag === HostComponent) { + const hostInstance: Instance = fiber.stateNode; + if (hostInstance !== null) { + detachDeletedInstance(hostInstance); + } + } + fiber.stateNode = null; + // I'm intentionally not clearing the `return` field in this level. We // already disconnect the `return` pointer at the root of the deleted // subtree (in `detachFiberMutation`). Besides, `return` by itself is not @@ -1400,15 +1412,6 @@ function detachFiberAfterEffects(fiber: Fiber) { // The purpose of this branch is to be super aggressive so we can measure // if there's any difference in memory impact. If there is, that could // indicate a React leak we don't know about. - - // For host components, disconnect host instance -> fiber pointer. - if (fiber.tag === HostComponent) { - const hostInstance: Instance = fiber.stateNode; - if (hostInstance !== null) { - detachDeletedInstance(hostInstance); - } - } - fiber.return = null; fiber.dependencies = null; fiber.memoizedProps = null; diff --git a/packages/react-reconciler/src/ReactFiberCommitWork.old.js b/packages/react-reconciler/src/ReactFiberCommitWork.old.js index 40df79a1b8528..f45a44083e236 100644 --- a/packages/react-reconciler/src/ReactFiberCommitWork.old.js +++ b/packages/react-reconciler/src/ReactFiberCommitWork.old.js @@ -1382,6 +1382,18 @@ function detachFiberAfterEffects(fiber: Fiber) { fiber.deletions = null; fiber.sibling = null; + // The `stateNode` is cyclical because on host nodes it points to the host + // tree, which has its own pointers to children, parents, and siblings. + // The other host nodes also point back to fibers, so we should detach that + // one, too. + if (fiber.tag === HostComponent) { + const hostInstance: Instance = fiber.stateNode; + if (hostInstance !== null) { + detachDeletedInstance(hostInstance); + } + } + fiber.stateNode = null; + // I'm intentionally not clearing the `return` field in this level. We // already disconnect the `return` pointer at the root of the deleted // subtree (in `detachFiberMutation`). Besides, `return` by itself is not @@ -1400,15 +1412,6 @@ function detachFiberAfterEffects(fiber: Fiber) { // The purpose of this branch is to be super aggressive so we can measure // if there's any difference in memory impact. If there is, that could // indicate a React leak we don't know about. - - // For host components, disconnect host instance -> fiber pointer. - if (fiber.tag === HostComponent) { - const hostInstance: Instance = fiber.stateNode; - if (hostInstance !== null) { - detachDeletedInstance(hostInstance); - } - } - fiber.return = null; fiber.dependencies = null; fiber.memoizedProps = null;