Skip to content

fix(TreeView): prune item-to-node map on collection Reset - #23634

Merged
MartinZikmund merged 3 commits into
dev/nr/alc-itemsview-debug-layout-leakfrom
dev/nr/alc-treeview-itemmap-reset-prune
Jul 6, 2026
Merged

MartinZikmund merged 3 commits into
dev/nr/alc-itemsview-debug-layout-leakfrom
dev/nr/alc-treeview-itemmap-reset-prune

Conversation

@nickrandolph

Copy link
Copy Markdown
Contributor

ALC pin-fix stack — PR 4 of 4 (uno). Base: dev/nr/alc-itemsview-debug-layout-leak. Top of the uno stack.

Fixes #23633

Copilot AI review requested due to automatic review settings July 3, 2026 13:15
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Nick Randolph seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@mergify

mergify Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a TreeView memory leak in content mode where an ItemsSource collection Clear (translated to a CollectionChange.Reset) removes nodes from the view but leaves m_itemToNodeMap entries behind, retaining cleared items for the TreeView’s lifetime (and potentially pinning collectible AssemblyLoadContexts), per #23633.

Changes:

  • On CollectionChange.Reset, prunes m_itemToNodeMap entries whose nodes are no longer attached to the origin node.
  • Adds helper logic to detect nodes detached from the origin root and uses it to prune stale mappings.
  • Prunes detached selections as an additional retention path (selected nodes/items surviving a reset).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@nickrandolph
nickrandolph force-pushed the dev/nr/alc-itemsview-debug-layout-leak branch from e652e9f to 6d9aae0 Compare July 3, 2026 13:41
@nickrandolph
nickrandolph force-pushed the dev/nr/alc-treeview-itemmap-reset-prune branch from 228b181 to cdcfce8 Compare July 3, 2026 13:42
@MartinZikmund
MartinZikmund self-requested a review July 3, 2026 18:28
Nick Randolph and others added 3 commits July 6, 2026 17:36
The WinUI-derived collection-change handling removes m_itemToNodeMap entries
only on ItemRemoved with an expanded parent. A Reset (ItemsSource collection
Clear) removed the nodes from view but left every item -> node mapping behind,
retaining the cleared items for the TreeView's lifetime — and when items come
from a collectible AssemblyLoadContext (plugin / preview hosting), pinning that
ALC. Prune mappings whose node is no longer attached to the origin node on
Reset; also covers removals under a collapsed parent once any reset occurs.
Companion to the item-to-node map prune: a selected node removed via a
collection Reset stayed in m_selectedNodes / m_selectedItems indefinitely,
keeping the node and its item alive (dump-proven: a TreeView-based tool's
selection vector retained the previous data session's items, pinning a
collectible ALC). SelectedItemsVector.RemoveAt keeps both vectors in sync and
raises selection-change notifications.
…lection tracking

Addresses review feedback (#23633): the Reset selection-pruning removed
via m_selectedItems.RemoveAt, bypassing TrackItemUnselected (RemoveAtCore
can't correlate an item already gone from SelectedItems), so
SelectionChanged wasn't raised in multi-select mode. Prune via
m_selectedNodes.RemoveAtCore (mirrors the ItemRemoved/Reset cases),
unsubscribe ChildrenChanged, and batch inside Begin/EndSelectionChanges
for a single aggregate SelectionChanged.

Co-Authored-By: Claude Fable 5 <[email protected]>
@nickrandolph
nickrandolph force-pushed the dev/nr/alc-itemsview-debug-layout-leak branch from 6d9aae0 to 55cd1af Compare July 6, 2026 07:37
@nickrandolph
nickrandolph force-pushed the dev/nr/alc-treeview-itemmap-reset-prune branch from cdcfce8 to a546bc1 Compare July 6, 2026 07:37
@MartinZikmund
MartinZikmund merged commit e326d83 into dev/nr/alc-itemsview-debug-layout-leak Jul 6, 2026
10 checks passed
@MartinZikmund
MartinZikmund deleted the dev/nr/alc-treeview-itemmap-reset-prune branch July 6, 2026 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants