Skip to content

Commit

Permalink
Fix a memory leak with the shared TreeWalker (#3888)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinfagnani committed May 9, 2023
1 parent 55148f8 commit 0f30e6f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-feet-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'lit-html': patch
---

Fix a memory leak cause by lit-html's shared TreeWalker holding a reference to the last tree it walked.
7 changes: 7 additions & 0 deletions packages/lit-html/src/lit-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,9 @@ class Template {
}
nodeIndex++;
}
// We could set walker.currentNode to another node here to prevent a memory
// leak, but every time we prepare a template, we immediately render it
// and re-use the walker in new TemplateInstance._clone().
debugLogEvent?.({
kind: 'template prep',
template: this,
Expand Down Expand Up @@ -1155,6 +1158,10 @@ class TemplateInstance implements Disconnectable {
nodeIndex++;
}
}
// We need to set the currentNode away from the cloned tree so that we
// don't hold onto the tree even if the tree is detached and should be
// freed.
walker.currentNode = d;
return fragment;
}

Expand Down

0 comments on commit 0f30e6f

Please sign in to comment.