Skip to content

Keep StatesChanged subscriptions in step in the VisualStateGroupList indexer - #38411

Closed
wbv-profile wants to merge 1 commit into
dotnet:mainfrom
wbv-profile:fix/weak-events-visualstate
Closed

wbv-profile wants to merge 1 commit into
dotnet:mainfrom
wbv-profile:fix/weak-events-visualstate

Conversation

@wbv-profile

Copy link
Copy Markdown
Contributor

Description of Change

VisualStateGroupList keeps its VisualStateGroup.StatesChanged subscriptions in step on every mutation path — Add, Insert, Remove, RemoveAt and Clear all subscribe or unsubscribe as appropriate. The indexer setter did not: it assigned straight through to the backing list.

That left two defects:

  • A leak. The subscription runs group → list (VisualStateGroup.StatesChanged holds the list's ValidateAndNotify), so a displaced group the app still holds kept the whole VisualStateGroupList alive, and with it the VisualElement that owns it. The displaced group also went on raising validation notifications for a list it was no longer part of.
  • A functional bug. The incoming group was never subscribed, so its subsequent state changes did not reach the list's validation at all.

The setter now unsubscribes the displaced group, assigns, and subscribes the incoming one, with a reference-equality short circuit so assigning the same instance stays a no-op. Null is handled without subscribing rather than by throwing, to avoid introducing a new exception on a path that previously accepted it.

Same defect class as #38397, #38403, #38405, #38408 and #38410, though the fix here is a missing unsubscribe rather than a weak-event conversion — the group and list are peers with a shared lifetime, so a plain subscription is correct as long as it is torn down.

Not covered here: the three related [leak-scan] reports about StateTriggerBase detachment (#38243, #38244, #38245) — triggers are only sent SendDetached() when the entire VisualStateGroups collection is replaced, not when an individual group or state is removed. Fixing that means changing when triggers detach and re-attach, which is a behavioural change worth its own PR rather than a rider on this one.

Tests

src/Controls/tests/Core.UnitTests/VisualStateGroupListIndexerTests.cs adds four tests. Reverting the source change fails two of them — DisplacedGroupDoesNotRootTheList (the leak) and ReplacingByIndexSubscribesTheIncomingGroup (the missed subscription). The other two pin behaviour the change must preserve: the displaced group no longer being validated by the list, and same-instance assignment being a no-op.

With the fix all four pass and the full Controls.Core.UnitTests suite is green.

Issues Fixed

Fixes #37171

…indexer

Add, Insert, Remove, RemoveAt and Clear all subscribe or unsubscribe
VisualStateGroup.StatesChanged as appropriate; the indexer setter
assigned straight through to the backing list instead.

The subscription runs group -> list, so a displaced group the app still
held kept the whole VisualStateGroupList -- and the VisualElement that
owns it -- alive, and went on raising validation notifications for a
list it had left. The incoming group, meanwhile, was never subscribed,
so its state changes never reached validation at all.

The setter now unsubscribes the displaced group and subscribes the
incoming one, with a reference-equality short circuit. Null is handled
without subscribing rather than by throwing, to avoid introducing a new
exception on a path that previously accepted it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 38411

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 38411"

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@kubaflo

kubaflo commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Closing this overlapping implementation in favor of #37340, already merged into inflight/current as 5793788.

The subscription repair here is useful, but the existing fix is more complete: it also calls ValidateAndNotify immediately after replacement, so the incoming group's names, visual-element association, state triggers and current visual state are updated without waiting for a later state mutation. Its regression test checks the visible margin changes immediately. This PR leaves that part unfixed.

This is duplication in inflight/current, not a claim that main already contains the fix. If a main backport is needed, we should port the existing implementation rather than maintain a second, narrower fix. The displaced-group GC regression would be useful as a focused follow-up to that implementation. Thanks for adding the explicit retention coverage.

@kubaflo kubaflo closed this Sep 10, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 27, 2026

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — e47a32d7 Deployed Sep 10, 2026 by wbv-profile via conclusion #1846
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[leak-scan] VisualStateGroupList.Item — index replacement leaves old StatesChanged subscription

2 participants