Skip to content

NavigationView: removed menu items are never collected while the NavigationView lives #25970

Closed

Description

@Xiaoy312

Current behavior 🐛

A NavigationViewItem, NavigationViewItemHeader or NavigationViewItemSeparator removed from a NavigationView is never garbage collected while the NavigationView lives. Its content and DataContext stay in memory with it. In an app whose shell NavigationView lives for the app's lifetime, every menu item ever removed (e.g. a dynamic MenuItemsSource that gets refreshed) accumulates.

The items listen to the NavigationView's SplitView (IsPaneOpen, DisplayMode, CompactPaneLength) through RegisterPropertyChangedCallback, which the SplitView holds strongly. Items only unregister when their template is re-applied, and headers and separators never unregister. Retention path: NavigationView → SplitView → property-changed registrations → callback → item.

Expected behavior 🎯

A menu item removed from the NavigationView can be collected once nothing else references it, as on WinUI, whose handlers don't keep the item alive.

How to reproduce it (as minimally and precisely as possible) 🔬

var nv = new NavigationView { PaneDisplayMode = NavigationViewPaneDisplayMode.Left, IsPaneOpen = true };
nv.MenuItems.Add(new NavigationViewItem { Content = "Kept" });
// show nv and wait for it to load

var weak = AddItem(nv); // [MethodImpl(NoInlining)]: adds a NavigationViewItem (or header/separator), returns a WeakReference to it
// wait for layout

nv.MenuItems.RemoveAt(1);
// wait for layout; remove one more item too, since ItemsRepeater (like WinUI) keeps the last cleared element in its cached ElementClearing args

GC.Collect(); GC.WaitForPendingFinalizers();
// weak.IsAlive is still true while nv is alive

Workaround 🛠️

None short of discarding the whole NavigationView.

Affected platforms 📱💻🖥️

All platforms 🌍

Uno.Sdk version (and other relevant versions) 📦

Current master. Found by code audit, following the same pattern fixed in AnimatedIcon by #25948.

IDE version 🧑‍💻

N/A

Anything else we need to know? 💬

Fix in #25968.

Activity

  1. added
    triage/untriaged
    on Nov 3, 2026
  2. added theissue type on Nov 3, 2026
  3. added a commit that references this issue on Nov 3, 2026
    3613bc0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage/untriaged

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions