Skip to content

fix(AnimatedIcon): Don't root the icon from its Foreground brush or State ancestor - #25948

Merged
Xiaoy312 merged 3 commits into
masterfrom
dev/xygu/20261102/animatedicon-foreground-leak
Nov 3, 2026
Merged

Xiaoy312 merged 3 commits into
masterfrom
dev/xygu/20261102/animatedicon-foreground-leak

Conversation

@Xiaoy312

@Xiaoy312 Xiaoy312 commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

GitHub Issue: closes unoplatform/kahua-private#563

PR Type:

🐞 Bugfix

What changed? 🚀

An AnimatedIcon whose Foreground is a long-lived SolidColorBrush (e.g. a ThemeResource brush) stayed in memory, together with its subtree and DataContext, for as long as the brush lived. AnimatedIcon listened to the brush's Color through the public RegisterPropertyChangedCallback, which the brush holds strongly until it's unregistered. The unload handler unregisters it, but an icon that was never loaded, or whose Foreground changed after it unloaded, kept a registration that nothing ever removed. The retention path is brush → _propertyChangedTokens → callback → AnimatedIcon.

The Color callback now goes through the internal RegisterDisposablePropertyChangedCallback, which holds it weakly and ties it to the icon's lifetime. WinUI behaves the same way: AnimatedIcon.cpp subscribes on every Foreground change with a { this, &AnimatedIcon::... } handler that doesn't own the icon.

The same applies to AnimatedIcon.State: on load, an icon copies State from the nearest ancestor that sets it and listens for that ancestor's changes. That registration was never removed, so a long-lived ancestor kept every icon ever loaded under it alive. It is now weak as well.

Scope. The public RegisterPropertyChangedCallback stays strong: that's its contract, and apps rely on a registered lambda staying alive. The bug is using it to listen to an object that can outlive the subscriber. When porting a WinUI handler that doesn't own its subscriber and listens to such an object, use RegisterDisposablePropertyChangedCallback. Other controls use the same pattern and will be looked at separately.

Known limitation. A brush keeps a strong reference to the first element it's assigned to (_associatedParent), so a brush used by exactly one element still keeps that element alive. That's independent of AnimatedIcon and out of scope here.

New runtime tests in Given_AnimatedIcon:

  • Leak tests, Uno-only ([PlatformCondition(Exclude, NativeWinUI)]):
    • When_Foreground_Set_While_Never_Loaded_Then_Not_Rooted_By_Brush, with and without a Source
    • When_Foreground_Changes_After_Unload_Then_Not_Rooted_By_Brush, with and without a Source
    • When_Unloaded_From_Ancestor_With_State_Then_Not_Rooted_By_Ancestor
  • Behavior tests, which also run on WinAppSDK for parity. The color tests observe the icon through a small IAnimatedVisualSource2 test double:
    • When_Loaded_Then_Brush_Color_Change_Reaches_Source
    • When_Foreground_Brush_Swapped_While_Loaded_Then_New_Brush_Is_Tracked
    • When_Reloaded_Then_Current_Color_Applied_And_Brush_Tracked
    • When_Ancestor_State_Changes_Then_Icon_State_Follows

Locally on Skia Win32 (SamplesApp net10.0-desktop):

  • Without the Foreground fix, the 4 Foreground leak cases fail.
  • Without the State fix, its leak test fails.
  • With both fixes, all 9 pass. Given_FrameworkElement_And_Leak and the AnimatedVisuals tests still pass (126/126 together with Given_AnimatedIcon).

On native WinUI (the WinAppSDK head, run locally), the 4 behavior tests pass and the 3 leak tests are skipped.

PR Checklist ✅

🤖 Generated with Claude Code

@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

@Xiaoy312
Xiaoy312 force-pushed the dev/xygu/20261102/animatedicon-foreground-leak branch from d1994ea to 3b52af6 Compare November 2, 2026 22:10
@Xiaoy312 Xiaoy312 changed the title fix(AnimatedIcon): Don't root the icon from its Foreground brush fix(AnimatedIcon): Don't root the icon from its Foreground brush or State ancestor Nov 2, 2026
@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 242999 found UI Test snapshots differences: skia-linux-screenshots: 2458, skia-windows-screenshots: 2458

Details
  • skia-linux-screenshots: 2458 changed over 2492

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • Accessibility_ScreenReader.png-dark
    • AnimatedBackVisualSourceSamplePage.png-dark
    • AnimatedBackVisualSourceSamplePage.png
    • AnimatedIconPage.png-dark
    • Accessibility_ScreenReader.png
    • AppBarToggleButtonTest.png
    • AnimatedVisualsTestbedPage.png-dark
    • AnimatedVisualsTestbedPage.png
    • AppBarToggleButtonTest.png-dark
    • AppBarButtonWithIconTest.png-dark
    • ApplicationViewSizing.png
    • ApplicationViewSpanningRectsPage.png-dark
    • Application_DispatcherShutdownMode.png-dark
    • ApplicationViewMode.png
    • AppBar_KeyBoard.png-dark
    • AppWindowPositionAndSize.png
    • AppWindowPositionAndSize.png-dark
    • AppWindowPresenters.png
    • AppWindowFeatures.png-dark
    • ApplicationViewSpanningRectsPage.png
  • skia-windows-screenshots: 2458 changed over 2492

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AndroidWindowInsets.png-dark
    • AppBarButtonTest.png-dark
    • AppBar_KeyBoard.png-dark
    • ApplicationViewSpanningRectsPage.png-dark
    • AppWindowFeatures.png
    • AppXamlDefinedResources.png-dark
    • Attributed_text_FontSize_Changing.png
    • AuthenticationBroker_Demo.png-dark
    • AutoBorderStretchwithtopmargin.png-dark
    • AutoBorderStretchwithtopmargin.png
    • AutoSizedPathCentered.png-dark
    • AutoSizedPathCentered.png
    • AutoSuggestBox_BitmapIcon.png-dark
    • AutoSuggestBox_BitmapIcon.png
    • AutoSuggestBox_SoftKeboard.png-dark
    • AutoSuggestBox_SoftKeboard.png
    • Basics_Automated.png-dark
    • Basics_Automated.png
    • BezierSegment.png-dark
    • BezierSegment.png

@Xiaoy312
Xiaoy312 force-pushed the dev/xygu/20261102/animatedicon-foreground-leak branch from 3b52af6 to 174c5d1 Compare November 2, 2026 23:49
Xiaoy312 and others added 3 commits November 2, 2026 18:52
An app-lifetime Foreground brush (a theme brush) must not keep an
AnimatedIcon alive, whether the icon was never loaded or was unloaded
before its Foreground changed. The icon must still track the brush's
Color while loaded, after a brush swap and after a reload.

Co-authored-by: jbourque-kahua <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The Color subscription went through the public token API, whose
registration the brush holds strongly, so an app-lifetime brush (a
ThemeResource) kept every icon that ever used it alive, with its
DataContext. WinUI's handler doesn't own the icon; register weakly to
match.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
An icon copies AnimatedIcon.State from the nearest ancestor that sets
it and listens for changes through the public token API, which the
ancestor holds strongly and nothing revokes, so a long-lived ancestor
kept every icon ever loaded under it alive. Register weakly, as for
the Foreground brush.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Xiaoy312
Xiaoy312 force-pushed the dev/xygu/20261102/animatedicon-foreground-leak branch from 174c5d1 to 38d9378 Compare November 2, 2026 23:52
@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 243009 found UI Test snapshots differences: skia-linux-screenshots: 2458, skia-windows-screenshots: 2458

Details
  • skia-linux-screenshots: 2458 changed over 2492

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedBackVisualSourceSamplePage.png-dark
    • AnimatedBackVisualSourceSamplePage.png
    • AppBarButtonTest.png-dark
    • AnimatedVisualsTestbedPage.png
    • AppBarToggleButtonTest.png
    • ApplicationViewSpanningRectsPage.png
    • AppWindowClosing.png-dark
    • AppWindowPositionAndSize.png-dark
    • AppWindowPresenters.png
    • AppXamlDefinedResources.png
    • Arrange_Performance01.png
    • Attributed_text_Supserscript.png-dark
    • AuthenticationBroker_Cancel.png-dark
    • AutoBorderStretchwithleftmargin.png-dark
    • AutoBorderStretchwithrightmargin.png-dark
    • AutoBorderStretchwithtopmargin.png
    • AutomationProperties_Relations.png-dark
    • AutomationProperties_StructureAndProps.png-dark
    • AutomationProperties_Relations.png
    • AutomationProperties_StructureAndProps.png
  • skia-windows-screenshots: 2458 changed over 2492

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • Accessibility_ScreenReader.png-dark
    • AndroidWindowInsets.png-dark
    • AppBar_KeyBoard.png-dark
    • AppBarButtonTest.png
    • ApplicationViewSpanningRectsPage.png
    • ApplicationViewMode.png
    • AppWindowPositionAndSize.png-dark
    • AppWindowClosing.png
    • Arrange_Performance01.png-dark
    • AppWindowTitleBar Properties.png-dark
    • AuthenticationBroker_Cancel.png-dark
    • ArcSegment.png
    • AutoBorderStretchwithleftmargin.png
    • AuthenticationBroker_Cancel.png
    • AutomationProperties_AutomationId.png
    • AutoBorderStretchwithtopmargin.png-dark
    • AutomationProperties_StructureAndProps.png-dark
    • AutomationProperties_Landmark.png-dark
    • AutomationProperties_StructureAndProps.png
    • AutomationProperties_Landmark.png

@Xiaoy312
Xiaoy312 enabled auto-merge November 3, 2026 01:29
@Xiaoy312
Xiaoy312 merged commit 3946c42 into master Nov 3, 2026
52 checks passed
@Xiaoy312
Xiaoy312 deleted the dev/xygu/20261102/animatedicon-foreground-leak branch November 3, 2026 04:18
@Xiaoy312

Xiaoy312 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

@Mergifyio backport release/stable/6.7 servicing/6.8

@mergify

mergify Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

backport release/stable/6.7 servicing/6.8

✅ Backports have been created

Details

Cherry-pick of 6fe9b9e has failed:

On branch mergify/bp/release/stable/6.7/pr-25948
Your branch is up to date with 'origin/release/stable/6.7'.

You are currently cherry-picking commit 6fe9b9e.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	added by them:   src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_AnimatedIcon.cs

no changes added to commit (use "git add" and/or "git commit -a")

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

Cherry-pick of 6fe9b9e has failed:

On branch mergify/bp/servicing/6.8/pr-25948
Your branch is up to date with 'origin/servicing/6.8'.

You are currently cherry-picking commit 6fe9b9e.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	added by them:   src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_AnimatedIcon.cs

no changes added to commit (use "git add" and/or "git commit -a")

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

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.

5 participants