Skip to content

[Skia] Shared dependency objects (style setter values, static brushes, input scopes, current tooltip) retain elements and DataContext of pages that are gone #25099

Open

Description

@mikernet

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:

  1. Transform.Changed handlers accumulate. Each element's NativeRenderTransformAdapter subscribes to the transform's Changed event and never unsubscribes when the element is unloaded or discarded. A static style's ScaleTransform therefore 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 inherited DataContext, a whole page view model and everything it owns.

  2. DataContext is propagated into shared setter values. A Transitions setter in a default style (reached through Style._defaultStyleCache) holds an AddDeleteThemeTransition. Uno pushes the element's DataContext into 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.

  3. 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 -> ViewFolderPageModel
    

    A 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.

  4. ToolTipService.m_CurrentToolTip keeps 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 -> ViewFolderPageModel
    

    Bounded 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

  1. A style with <Setter Property="RenderTransform"><Setter.Value><ScaleTransform .../></Setter.Value></Setter> applied to a FontIcon used inside pages.
  2. Navigate between pages that contain such icons; the pages are not cached.
  3. 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

  1. mikernet commented on Oct 8, 2026

    @mikernet
    ContributorAuthor

    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_CurrentToolTip is assigned in OpenToolTipImpl and only ever replaced by the next tooltip that opens. CloseToolTipImpl and OnCloseTimerTick set IsOpen = false but never clear the field, so the last tooltip that opened keeps its _owner element, 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.

  2. mikernet commented on Oct 8, 2026

    @mikernet
    ContributorAuthor

    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):

    Before With #25102
    Departed pages still alive after ~60 navigations 16 to 29 3
    Roots holding them static brushes, a default-style AddDeleteThemeTransition, ToolTipService.m_CurrentToolTip, style-shared ScaleTransform handlers only the navigator's own back/forward cache
    Managed heap after collection 350 to 900 MB 60 to 130 MB

    One 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 TransitionCollection holding the last page it was applied to (bounded to one page); it is fixed on master by the weak cached value.

  3. added
    7.1
    on Oct 14, 2026
  4. removed
    7.1
    on Oct 29, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions