Repository navigation
Description
Current behavior
A Style setter whose value is a dependency object (RenderTransform with a ScaleTransform, Transitions with a TransitionCollection) assigns that single instance to every element the style is applied to. On Skia this makes the shared instance a memory leak root in two ways:
-
Transform.Changedhandlers accumulate. Each element'sNativeRenderTransformAdaptersubscribes to the transform'sChangedevent and never unsubscribes when the element is unloaded or discarded. A static style'sScaleTransformtherefore holds a strong handler for every element ever styled with it. Reference chain found in our app with a reflection walk from static fields:CrumbBar.<MenuIconStyle>k__BackingField [static] -> Style _flattenedSetters -> SetterBase[] -> [6] -> Setter _value -> ScaleTransform Changed -> EventHandler -> delegates -> Delegate[] -> [147] -> EventHandler _target -> NativeRenderTransformAdapter <Owner>k__BackingField -> FontIcon __storeBackingField -> DependencyObjectStore -> _properties -> _dataContextPropertyDetails _value -> ViewFolderPageModel (a page navigated away from long ago)147 handlers after a few minutes of navigating; each roots a
FontIcon, and through its inheritedDataContext, a whole page view model and everything it owns. -
DataContext is propagated into shared setter values. A
Transitionssetter in a default style (reached throughStyle._defaultStyleCache) holds anAddDeleteThemeTransition. Uno pushes the element'sDataContextinto it, so the shared transition keeps the last page model that applied the style alive until another element overwrites it:Microsoft.UI.Xaml.Style._defaultStyleCache [static] -> Dictionary`2 -> _entries -> [16].value -> Style _flattenedSetters -> [11] -> Setter -> _value -> TransitionCollection -> _items -> [0] -> AddDeleteThemeTransition __storeBackingField -> DependencyObjectStore -> _properties -> _dataContextPropertyDetails _value -> ViewSlidesPageModel (a page navigated away from)This one is bounded (one model per shared transition) but still retains pages that should be collectable.
-
The same propagation applies to any shared dependency object assigned from code, not just style setters. Three more chains from the same walk, each holding a page that was navigated away from:
FieldInputControl.<ErrorBrush>k__BackingField [static] -> SolidColorBrush __storeBackingField -> DependencyObjectStore -> _properties -> _dataContextPropertyDetails _value -> ViewLinearFormPageModel FieldInput.<DefaultScope>k__BackingField [static] -> InputScope __storeBackingField -> DependencyObjectStore -> _properties -> _dataContextPropertyDetails _value -> FieldInputModel -> _signAsync -> Func`1 -> _target -> FormRunnerModel -> <sessionInfoProvider>P -> RepoRootModel Microsoft.UI.Xaml.Style._defaultStyleCache [static] -> ... -> Setter -> _value -> DesktopAcrylicBackdrop __storeBackingField -> DependencyObjectStore -> _associatedParent -> MenuFlyoutPresenter _items -> ItemCollection -> _itemsSource -> Object[] -> [1] -> MenuFlyoutItem m_epCanExecuteChangedHandler -> ... -> AsyncRelayCommand -> execute -> Func`1 -> _target -> ViewFolderPageModelA brush, input scope, or backdrop that is shared between elements (which is the normal way to use brushes, including every
{ThemeResource}brush) retains the DataContext, or the parent, of the last element it was assigned to. Each such object holds one page, so this part is bounded, but an app has dozens of shared brushes and the pages they pin can be large. -
ToolTipService.m_CurrentToolTipkeeps the last tooltip's owner after the owner's page is gone:Microsoft.UI.Xaml.Controls.ToolTipService.m_CurrentToolTip [static] -> ToolTip _owner -> Button __storeBackingField -> DependencyObjectStore -> _properties -> _dataContextPropertyDetails _inheritedValue -> ViewFolderPageModelBounded to one, but the same shape: a static holding a dead page through an element's inherited DataContext.
WinUI shares setter values the same way, but there neither the transform nor the transition keeps strong references back to the elements, so the pattern is leak-free and common in app styles.
Expected behavior
Elements that are unloaded or discarded must not stay reachable from a style's setter value. Concretely: NativeRenderTransformAdapter (and anything else subscribing to Transform.Changed) should unsubscribe when the owner is unloaded or the transform is replaced, or subscribe weakly; and DataContext should not be pushed into objects that are shared setter values (transitions, transforms, brushes), or should be cleared when the element leaves the tree.
How these were found
A reflection walk over the object graph from every static field of every loaded type (Mono on WebAssembly has no heap dump), run after a forced full collection, targeting page view models that had been navigated away from. All chains above are verbatim from that walk; the app does not cache those pages.
Repro
- A style with
<Setter Property="RenderTransform"><Setter.Value><ScaleTransform .../></Setter.Value></Setter>applied to aFontIconused inside pages. - Navigate between pages that contain such icons; the pages are not cached.
- After a forced collection, the departed pages' view models are still alive; the chain above leads to them from the style.
Environment
- Uno.WinUI 6.8.0-dev.212, Uno.Sdk 6.8.0-dev.60
- .NET 10.0.401, runtime pack 10.0.12
- Heads: net10.0-browserwasm (Skia renderer) and net10.0-desktop
Activity
mikernet commentedon Oct 8, 2026
On item 4 (
ToolTipService.m_CurrentToolTip): this is independent of #24592, which changes where an open ToolTip takes its DataContext from but does not touch the static.m_CurrentToolTipis assigned inOpenToolTipImpland only ever replaced by the next tooltip that opens.CloseToolTipImplandOnCloseTimerTicksetIsOpen = falsebut never clear the field, so the last tooltip that opened keeps its_ownerelement, and through that element's inherited DataContext the page it was on, rooted until some other tooltip opens anywhere in the app. Clearing the field when the current tooltip closes (and when its owner is unloaded) would remove that chain.fix(transform): Stop shared transforms rooting discarded elements
fix(tooltip): Release the current ToolTip once it closes
fix(dependencyobject): Make shared value links to owners non-owning
fix(dependencyobject): Make shared value links to owners non-owning
fix(dependencyobject): Make shared value links to owners non-owning
fix(dependencyobject): Make shared value links to owners non-owning
fix(transform): Stop shared transforms rooting discarded elements
fix(tooltip): Release the current ToolTip once it closes
fix(dependencyobject): Make shared value links to owners non-owning
fix(transform): Stop shared transforms rooting discarded elements
fix(tooltip): Release the current ToolTip once it closes
fix(dependencyobject): Make shared value links to owners non-owning
fix(dependencyobject): Make shared value links to owners non-owning
fix(transform): Stop shared transforms rooting discarded elements
fix(tooltip): Release the current ToolTip once it closes
fix(dependencyobject): Make shared value links to owners non-owning
mikernet commentedon Oct 8, 2026
Measured result of #25102 in the app this was reported from (ported onto 6.8.0-dev.212 and run in the WebAssembly head, same reflection walk from static and thread-static roots after forced collections):
AddDeleteThemeTransition,ToolTipService.m_CurrentToolTip, style-sharedScaleTransformhandlersOne note for backporting: the 6.8 port could carry everything except the weak inherited DataContext. On 6.8 a non-FrameworkElement's inherited DataContext is a real dependency property value, and the only weak-storage facility there applies to every precedence of the property, so it was left strong and the clears on unassociation and on a second parent do the work. The one chain that remains on 6.8 is the default-style
TransitionCollectionholding the last page it was applied to (bounded to one page); it is fixed on master by the weak cached value.fix(transform): Stop shared transforms rooting discarded elements
fix(dependencyobject): Hold the inherited DataContext weakly
fix(transform): Stop shared transforms rooting discarded elements
fix(dependencyobject): Hold a value's inherited DataContext weakly
fix(dependencyobject): Drop a collected parent's DataContext
fix(transform): Stop shared transforms rooting discarded elements
fix(dependencyobject): Hold the inherited DataContext weakly