Skip to content

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

Merged
agneszitte merged 4 commits into
release/stable/6.7from
mergify/bp/release/stable/6.7/pr-25948
Nov 4, 2026
Merged

agneszitte merged 4 commits into
release/stable/6.7from
mergify/bp/release/stable/6.7/pr-25948

Conversation

@mergify

@mergify mergify Bot commented Nov 3, 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


This is an automatic backport of pull request #25948 done by [Mergify](https://mergify.com).

Xiaoy312 and others added 3 commits November 3, 2026 06:45
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]>
(cherry picked from commit 6fe9b9e)

# Conflicts:
#	src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_AnimatedIcon.cs
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]>
(cherry picked from commit 6bcfdd7)
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]>
(cherry picked from commit 38d9378)
@mergify mergify Bot added the conflicts Automated merge hit conflicts label Nov 3, 2026
@mergify

This comment was marked as resolved.

Move Given_AnimatedIcon next to the other AnimatedVisual tests, matching
its namespace, and implement the Uno-specific IAnimatedVisualSource
members on the test's spy source, which 6.x still declares.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@unodevops

Copy link
Copy Markdown
Contributor

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

@Xiaoy312 Xiaoy312 removed the conflicts Automated merge hit conflicts label Nov 3, 2026
@Xiaoy312
Xiaoy312 enabled auto-merge November 3, 2026 19:21
@mergify

mergify Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor Author

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

@agneszitte
agneszitte disabled auto-merge November 4, 2026 13:45
@agneszitte
agneszitte merged commit e437bb3 into release/stable/6.7 Nov 4, 2026
45 checks passed
@agneszitte
agneszitte deleted the mergify/bp/release/stable/6.7/pr-25948 branch November 4, 2026 13:46
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.

3 participants