Skip to content

Commit

Permalink
Fix memory leak in plugin tree views (#12353)
Browse files Browse the repository at this point in the history
Fixes a bug in `TreeViewExtImpl<T>.getChildren(parentId: string)`,
where newly created top-level children were added to the old root node
instead of the new one. This caused a memory leak by retaining an implicit
reference to the old root node from the new one and failing to properly track
and, hence, dispose the top-level children.

The fix ensures that old nodes get disposed and become subject to garbage
collection by reusing the single root node instead of creating a new one
on full refresh.

See #10404 (comment)
for more information.

Fixes #10404.
  • Loading branch information
pisv committed Apr 12, 2023
1 parent 2b81f49 commit b56f399
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/plugin-ext/src/plugin/tree/tree-views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ class TreeViewExtImpl<T> implements Disposable {
}

async getChildren(parentId: string): Promise<TreeViewItem[] | undefined> {
const parentNode = this.nodes.get(parentId);
let parentNode = this.nodes.get(parentId);
const parent = parentNode?.value;
if (parentId && !parent) {
console.error(`No tree item with id '${parentId}' found.`);
Expand All @@ -398,9 +398,10 @@ class TreeViewExtImpl<T> implements Disposable {
this.clearChildren(parentNode);

// place root in the cache
if (parentId === '') {
if (parentId === '' && !parentNode) {
const rootNodeDisposables = new DisposableCollection();
this.nodes.set(parentId, { id: '', disposables: rootNodeDisposables, dispose: () => { rootNodeDisposables.dispose(); } });
parentNode = { id: '', disposables: rootNodeDisposables, dispose: () => { rootNodeDisposables.dispose(); } };
this.nodes.set(parentId, parentNode);
}
// ask data provider for children for cached element
const result = await this.options.treeDataProvider.getChildren(parent);
Expand Down

0 comments on commit b56f399

Please sign in to comment.