Repository navigation
Fix [leak-scan] VisualStateGroupList.Clear — removed groups keep triggers attached - #38439
Conversation
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 38439Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 38439" |
|
Hey there @@KarthikRajaKalaimani! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
|
Azure Pipelines: Successfully started running 1 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
MauiBot
left a comment
There was a problem hiding this comment.
Expert Review — 2 findings
See inline comments for details.
addressed Ai summary concerns. |
This comment has been minimized.
This comment has been minimized.
|
Note 🔍
|
This comment has been minimized.
This comment has been minimized.
Tests Failure Analysis
🧪 CI Analysis — click to expand📊 maui-pr
🧪 maui-pr-devicetestsNo results available. 🧪 maui-pr-uitestsNo results available. 🧭 Follow-up — actions and refreshNext action: Comment
|
MauiBot
left a comment
There was a problem hiding this comment.
AI Review Summary
@KarthikRajaKalaimani — new AI review results are available based on commit
2036dff.
🗂️ Review Sessions — click to expand
🚦 Gate — Test Before & After Fix
Gate Result: ✅ PASSED
Platform: ANDROID · Base: main · Merge base: f8a2f062
| Test | Without Fix (expect FAIL) | With Fix (expect PASS) |
|---|---|---|
🧪 VisualStateManagerTests VisualStateManagerTests |
✅ FAIL — 116s | ✅ PASS — 69s |
🔴 Without fix — 🧪 VisualStateManagerTests: FAIL ✅ · 116s
Error-relevant lines (filtered from the build log):
at Microsoft.Maui.Controls.Core.UnitTests.VisualStateManagerTests.ReplacingGroupByIndexDetachesStateTriggers() in /_/src/Controls/tests/Core.UnitTests/VisualStateManagerTests.cs:line 399
at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
at Microsoft.Maui.Controls.Core.UnitTests.VisualStateManagerTests.ClearDetachesStateTriggers() in /_/src/Controls/tests/Core.UnitTests/VisualStateManagerTests.cs:line 346
at Microsoft.Maui.Controls.Core.UnitTests.VisualStateManagerTests.RemoveDetachesStateTriggers() in /_/src/Controls/tests/Core.UnitTests/VisualStateManagerTests.cs:line 356
at Microsoft.Maui.Controls.Core.UnitTests.VisualStateManagerTests.AddingDuplicateNamedGroupDetachesReplacedStateTriggers() in /_/src/Controls/tests/Core.UnitTests/VisualStateManagerTests.cs:line 389
at Microsoft.Maui.Controls.Core.UnitTests.VisualStateManagerTests.RemoveAtDetachesStateTriggers() in /_/src/Controls/tests/Core.UnitTests/VisualStateManagerTests.cs:line 379
🟢 With fix — 🧪 VisualStateManagerTests: PASS ✅ · 69s
(no coded error found; showing last 1200 chars)
1 ms]
Passed IsEnabledToggleWhileSelectedPreservesState [5 ms]
Passed SelectHoverDeselectRestoresPointerOverState [< 1 ms]
Passed StateNamesMustBeUniqueWithinGroupList [< 1 ms]
Passed VisualStateGroupsFromSettersAreDistinct [< 1 ms]
Passed RemoveNonMemberReturnsFalse [< 1 ms]
Passed ImplicitStyleDisabledVSMOverridesLocalValue [1 ms]
Passed VisualElementGoesToCorrectStateWhenAvailable [< 1 ms]
Passed SelectingWhileDisabledStaysDisabledUntilReEnabled [< 1 ms]
Passed StateNamesInGroupMayNotBeEmpty [< 1 ms]
Passed CanRemoveAStateAndAddANewStateWithTheSameName [< 1 ms]
[xUnit.net 00:00:03.35] Finished: Microsoft.Maui.Controls.Core.UnitTests
Passed VSMFromStyleAreUnApplied [1 ms]
Passed UnapplyingVSMShouldUnapplySetters [< 1 ms]
Passed AddingDuplicateNamedGroupDetachesReplacedStateTriggers [< 1 ms]
Passed RemoveAtDetachesStateTriggers [< 1 ms]
Passed StateNamesInGroupMayNotBeNull [< 1 ms]
Passed VerifyVisualStateChanges [< 1 ms]
Passed AssigningSameIsItemSelectedValueIsNoOp [< 1 ms]
Passed VisualElementGoesToCorrectStateWhenAvailableFromSetter [< 1 ms]
Test Run Successful.
Total tests: 35
Passed: 34
Skipped: 1
Total time: 4.6141 Seconds
📁 Fix files reverted (1 files)
src/Controls/src/Core/VisualStateManager.cs
📋 Pre-Flight — Context & Validation
PR #38439 Pre-Flight
Context
- PR:
Fix [leak-scan] VisualStateGroupList.Clear — removed groups keep triggers attached - Issue: #38245
- Platform: Android (the defect is in shared Controls code and affects all platforms)
- Base:
main - Materialized review commit:
1f2fcef2bbcf0484c82e6f5460674113544c4fe8 - Merge base:
f8a2f062a8e1b4686a5a3de93bb69cabc02c8c4a
An attached StateTriggerBase can subscribe to a long-lived publisher from
OnAttached(). Removing its VisualStateGroup through an existing
VisualStateGroupList does not call SendDetached(), so OnDetached() cannot
unsubscribe and the publisher continues retaining the trigger and page-local
state.
Affected mutation paths are Clear, Remove, RemoveAt, duplicate-name
replacement in Add, and indexer replacement.
Existing PR Approach
The PR adds VisualStateGroupList.DetachGroup, which unsubscribes
StatesChanged, calls SendDetached() for each nested trigger, and clears the
group's VisualElement. It routes every removal/replacement path through that
helper. The PR also changes Remove to locate the stored instance and adds five
trigger-detachment tests plus a non-member removal test.
Changed files:
src/Controls/src/Core/VisualStateManager.cssrc/Controls/tests/Core.UnitTests/VisualStateManagerTests.cs
Alternative candidates must avoid repeating the PR's centralized
DetachGroup-called-from-each-list-mutation strategy and may modify only
src/Controls/src/Core/VisualStateManager.cs; the regression tests are
read-only.
Verification Surface
The prior gate already proved the tests fail without the fix and pass with it:
five added regression tests failed at baseline, while the fixed class suite
passed 34 tests with one skipped.
Run only:
dotnet test src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj --filter "FullyQualifiedName~VisualStateManagerTests" --logger "console;verbosity=normal"This class-level primary test includes the five new regression cases and the
existing adjacent Visual State Manager tests. No additional mandatory
regression test command was specified. Do not rerun gate verification or run a
broader suite.
Constraints
- Use
EstablishBrokenBaseline.ps1and its restore mode exactly as required by
thetry-fixskill. - Preserve
gate/content.md. - One initial implementation/test pass and at most one focused
correction/retest. - Perform the skill's expert self-review inline; do not launch another agent.
- Persist all attempt artifacts before restoring the source file.
🔬 Code Review — Deep Analysis
Code Review — PR #38439
Independent Assessment
What this changes: The submitted fix routes VisualStateGroupList removal and replacement paths through DetachGroup, which unsubscribes StatesChanged, detaches nested state triggers, and clears the removed group's VisualElement. It also makes Remove resolve the actual stored group and validates/wires index replacements.
Inferred motivation: A removed trigger can remain subscribed to a long-lived publisher after its group leaves an attached list, retaining page-local state.
Expert Reviewer Result
The single maui-expert-reviewer invocation returned LGTM with medium confidence and no new actionable inline findings. It confirmed that SendDetached() is idempotent and that the submitted regression tests cover the reported trigger-retention paths. The required inline-findings.json was written as [].
Prior Review Reconciliation
| Prior error finding | Status | Evidence |
|---|---|---|
Removing a non-member called RemoveAt(-1) instead of returning false |
✅ Fixed | Current Remove returns false when IndexOf is negative, and RemoveNonMemberReturnsFalse covers the contract. |
Missing negative-case coverage for Remove |
✅ Fixed | RemoveNonMemberReturnsFalse is present in the submitted HEAD. |
| Active visual-state setters survive group removal | ❌ Unresolved | DetachGroup detaches triggers and clears group.VisualElement without unapplying setters from group.CurrentState; whole-property replacement already performs equivalent setter cleanup. |
Failure-Mode Probing
- Clear/remove an active group: Trigger subscriptions are released, but the active state's setter contributions remain applied after the defining group is gone.
- Duplicate-name/index replacement: The outgoing group is detached before the replacement is installed, so stale effective values can remain from the removed group.
- Repeated detach:
StateTriggerBase.SendDetached()is guarded byIsAttached, so repeated lifecycle cleanup does not duplicateOnDetached()calls. - Non-member removal: The current implementation returns
falsewithout detaching or mutating the stored group.
CI Status
The trusted Gate passed: the focused regressions fail without the submitted fix and pass with it. GitHub required-check status could not be collected because gh is unauthenticated; no claim is made that all PR CI is green.
External Output Contract
Not applicable.
Verdict: NEEDS_CHANGES
Confidence: low
Summary: The submitted fix closes the reported trigger-subscription leak, and the current expert reviewer found no new changed-line defect. However, the mandatory prior-review reconciliation leaves a concrete earlier error unresolved: collection-level teardown does not relinquish setters from an active removed group. A reviewer-enhanced candidate should address that adjacent lifecycle invariant before comparison.
🛠️ Try-Fix — Analysis & Comparison
Alternative Fix Candidates for PR #38439
Candidate 1 — VisualStateGroup ownership-transition teardown
Result: FAIL
This candidate moved trigger teardown away from explicit collection-mutation
traversal and into VisualStateGroup.VisualElement ownership changes. It
compiled after one focused correction, then failed all five new detachment
tests (29 passed, 5 failed, 1 skipped).
The mechanism is insufficient because a trigger can be directly attached while
its group owner remains null. Removing that group produces no ownership
transition, so the teardown gate is never entered. Candidate 2 must not depend
on a prior non-null VisualElement and must not repeat either this ownership
strategy or the PR's centralized mutation-path DetachGroup strategy.
Candidate report (including its complete diff):
CustomAgentLogsTmp/PRState/38439/PRAgent/try-fix-1/content.md
Attempt artifacts:
CustomAgentLogsTmp/PRState/38439/PRAgent/try-fix/attempt-1/
Candidate 2 — Backing-collection removal notifications
Result: PASS
This candidate extends WatchAddList<T> with an optional removal callback and
configures VisualStateGroupList to tear down the actual stored group in
response to structural removal. This differs from both prior mechanisms:
public mutation methods do not explicitly invoke teardown, and teardown does
not depend on a prior non-null VisualElement.
The single permitted test run passed all five new regression scenarios and the
adjacent class tests: 35 total, 34 passed, 0 failed, and 1 skipped. Inline
expert self-review found no issues.
The key mechanism is that WatchAddList<T> removes or replaces an entry first,
then reports the stored instance. OnGroupRemoved balances one
StatesChanged subscription and detaches nested triggers only after the final
reference to that exact group leaves the list. This preserves duplicate
reference behavior while covering directly attached, null-owned triggers.
Candidate report (including its complete diff):
CustomAgentLogsTmp/PRState/38439/PRAgent/try-fix-2/content.md
Attempt artifacts:
CustomAgentLogsTmp/PRState/38439/PRAgent/try-fix/attempt-2/
Aggregate Outcome
| Candidate | Mechanism | Result | Self-review |
|---|---|---|---|
| 1 | Detach on VisualStateGroup.VisualElement ownership loss |
FAIL — 5 regression failures after one correction | 1 major finding |
| 2 | Detach from WatchAddList<T> structural-removal notification |
PASS — 34 passed, 1 skipped | Clean |
Candidate 2 is a viable alternative under the bounded validation performed in
STEP 5a. Candidate 1 is not viable because trigger attachment does not imply a
non-null group owner.
🏁 Report — Final Recommendation
⚠️ Final Recommendation: REQUEST CHANGES
Winner
pr-plus-reviewer
The submitted PR correctly fixes the reported trigger-retention paths and passes the trusted Gate, but mandatory prior-review reconciliation confirmed an unresolved adjacent teardown defect: removing an active VisualStateGroup leaves its setters applied after the group is gone. pr-plus-reviewer preserves the submitted leak fix, adds symmetric active-state cleanup and safe group reuse, and passed the one authorized targeted validation run. Because the winning changes are not in the submitted PR HEAD, the recommendation is REQUEST CHANGES.
Candidate comparison
| Rank | Candidate | Mechanism | Regression evidence | Assessment |
|---|---|---|---|---|
| 1 | pr-plus-reviewer |
Raw PR plus shared current-state setter cleanup, CurrentState reset, and focused reuse coverage |
PASS — 35 passed, 1 skipped | Only candidate that fixes the trigger leak and relinquishes the removed active state's effective values without breaking group reuse. |
| 2 | pr |
Explicit DetachGroup teardown across Clear, Remove, RemoveAt, duplicate-name Add, and index replacement |
PASS — trusted Gate, 34 passed and 1 skipped | Correct for the reported leak and non-member Remove contract, but leaves active-state setter contributions behind. |
| 3 | try-fix-2 |
WatchAddList<T> structural-removal callbacks drive exact-instance trigger teardown |
PASS — 34 passed, 1 skipped | Viable for the leak, but broadens a shared collection abstraction and still does not unapply setters from an active removed group. |
| 4 | try-fix-1 |
Trigger teardown on VisualStateGroup.VisualElement ownership loss |
FAIL — 5 target regressions failed; 29 passed, 1 skipped | Directly attached triggers can have a null owner, so null-to-null removal never enters the teardown gate. |
The regression-failing candidate is ranked below every candidate with passing regression evidence, as required.
Expert and prior-review reconciliation
The single expert-review invocation returned LGTM with medium confidence and wrote inline-findings.json as []. It found no new changed-line issue in the current submitted HEAD. Review of all three prior-comment surfaces found three earlier errors:
- The non-member
RemoveAt(-1)regression is fixed by the currentindex < 0return. - Missing non-member coverage is fixed by
RemoveNonMemberReturnsFalse. - Active visual-state setters surviving group removal is still applicable: submitted
DetachGroupclears ownership without calling the same setter cleanup used by whole-property replacement.
Under the code-review consistency rules, that unresolved prior error overrides the current no-finding result.
Winning patch
pr-plus-reviewer factors whole-property cleanup into VisualStateManager.UnapplyStateSetters, invokes it from collection teardown before clearing VisualElement, and resets CurrentState so a removed group can be re-added and apply normally. ClearUnappliesAndReappliesCurrentStateSetters covers restoration of the old effective value and reapplication after reuse.
git diff --check 1f2fcef2bbcf0484c82e6f5460674113544c4fe8 passed before validation. The exact required class-level test command ran once from /home/vsts/work/_temp/pr-38439-pr-plus-reviewer and passed all runnable tests: 36 total, 35 passed, 1 skipped.
Evidence and uncertainty
- Raw PR trusted Gate: PASS; not rerun.
pr-plus-reviewer: PASS; one authorized focused run.try-fix-2: PASS under STEP 5a's bounded class-level run.try-fix-1: FAIL with all five new detachment regressions failing.- Mechanical regression cross-reference: clean.
- GitHub required checks could not be queried because
ghis unauthenticated; this report does not claim all PR CI is green.
Apply pr-plus-reviewer/reviewer.patch to the submitted PR.
📱 UI Tests — VisualStateManager
Detected UI test categories: VisualStateManager
✅ Deep UI tests — 97 passed, 0 failed, 4 skipped across 1 category on platform-pool agent (replaces in-process counts above).
🧪 UI Test Execution Results (deep, platform pool)
| Category | Tests | Snapshot diffs |
|---|---|---|
VisualStateManager |
97/101 (4 skipped) ✓ | — |
📎 Download drop-deep-uitests artifact (TRX + snapshot diffs) |
🧭 Next Steps — reviewer changes required
The reviewer-enhanced candidate identified changes that are not yet in the submitted PR.
Why: The reviewer-enhanced PR preserves the Gate-validated trigger-detachment fix and also removes active-state setter contributions before group ownership is cleared. Its single authorized VisualStateManager test run passed 35 tests with 1 skip, while the raw PR and try-fix-2 leave that teardown invariant unresolved.
Address the actionable findings in this review before merging.
kubaflo
left a comment
There was a problem hiding this comment.
Could you please resolve conflicts and check the ai's suggestions?
|
/azp run |
|
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command. |
Preserve group trigger teardown alongside upstream state-trigger removal and indexer subscription handling. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
Resolved the conflict in This PR is still needed. #37340 fixes group subscription handling, and #38444 detaches triggers removed from Verified against that exact current target using the full fail-without/pass-with script:
GitHub now reports no merge conflicts. CI for the new head has not completed; this is not a CI-green or merge-approval claim. |
…ggers attached (#38439) <!-- Please let the below note in for people that find this PR --> > [!NOTE] > Are you waiting for the changes in this PR to be merged? > It would be very helpful if you could [test the resulting artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from this PR and let us know in a comment if this change resolves your issue. Thank you! ### Issue Details Clearing an attached VisualStateGroupList removes groups without detaching the state triggers nested inside them. A trigger subscribed to a shared managed publisher in OnAttached() therefore stays subscribed and retained after its group is removed. ### Root Cause An attached `StateTriggerBase` runs `OnAttached()`, where custom triggers may subscribe to long-lived publishers such as static events. They rely on `SendDetached()` invoking `OnDetached()` to unsubscribe. Replacing the entire `VisualStateGroups` attached property already performed this teardown. However, mutating the existing `VisualStateGroupList` did not: - `Clear` - `Remove` - `RemoveAt` - Adding a group that replaces one with the same name - Replacing a group through the indexer These paths only removed `StatesChanged` handlers and collection entries. Nested triggers remained attached, leaving retention paths such as: ```text static publisher → event delegate → state trigger → page-local data ``` ### Description of Change Added a shared `DetachGroup` teardown method that: 1. Unsubscribes the group's `StatesChanged` handler. 2. Calls `SendDetached()` on every nested state trigger. 3. Removes the group's reference to its associated visual element. All removal and replacement paths in `VisualStateGroupList` now use this teardown. The `Remove` implementation also identifies the actual stored group before removing it. This ensures the correct instance is detached even when another equality-equivalent object is supplied. ## Regression coverage Added tests to `VisualStateManagerTests.cs` verifying trigger detachment for: - `Clear` - `Remove` - `RemoveAt` - Duplicate-name replacement through `Add` - Index replacement ### Issues Fixed Fixes #38245 **Tested the behavior in the following platforms.** - [x] Android - [x] Windows - [x] iOS - [x] Mac | Before | After | |---------|--------| | <img width="300" height="600" alt="image" src="https://github.com/user-attachments/assets/3f5ede16-f0ee-4aac-b728-07835001f851" /> | <img width="300" height="600" alt="image" src="https://github.com/user-attachments/assets/dafc0ce1-e9b4-439d-949e-cbe29a29af49" /> | --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…ggers attached (#38439) <!-- Please let the below note in for people that find this PR --> > [!NOTE] > Are you waiting for the changes in this PR to be merged? > It would be very helpful if you could [test the resulting artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from this PR and let us know in a comment if this change resolves your issue. Thank you! ### Issue Details Clearing an attached VisualStateGroupList removes groups without detaching the state triggers nested inside them. A trigger subscribed to a shared managed publisher in OnAttached() therefore stays subscribed and retained after its group is removed. ### Root Cause An attached `StateTriggerBase` runs `OnAttached()`, where custom triggers may subscribe to long-lived publishers such as static events. They rely on `SendDetached()` invoking `OnDetached()` to unsubscribe. Replacing the entire `VisualStateGroups` attached property already performed this teardown. However, mutating the existing `VisualStateGroupList` did not: - `Clear` - `Remove` - `RemoveAt` - Adding a group that replaces one with the same name - Replacing a group through the indexer These paths only removed `StatesChanged` handlers and collection entries. Nested triggers remained attached, leaving retention paths such as: ```text static publisher → event delegate → state trigger → page-local data ``` ### Description of Change Added a shared `DetachGroup` teardown method that: 1. Unsubscribes the group's `StatesChanged` handler. 2. Calls `SendDetached()` on every nested state trigger. 3. Removes the group's reference to its associated visual element. All removal and replacement paths in `VisualStateGroupList` now use this teardown. The `Remove` implementation also identifies the actual stored group before removing it. This ensures the correct instance is detached even when another equality-equivalent object is supplied. ## Regression coverage Added tests to `VisualStateManagerTests.cs` verifying trigger detachment for: - `Clear` - `Remove` - `RemoveAt` - Duplicate-name replacement through `Add` - Index replacement ### Issues Fixed Fixes #38245 **Tested the behavior in the following platforms.** - [x] Android - [x] Windows - [x] iOS - [x] Mac | Before | After | |---------|--------| | <img width="300" height="600" alt="image" src="https://github.com/user-attachments/assets/3f5ede16-f0ee-4aac-b728-07835001f851" /> | <img width="300" height="600" alt="image" src="https://github.com/user-attachments/assets/dafc0ce1-e9b4-439d-949e-cbe29a29af49" /> | --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…ggers attached (#38439) <!-- Please let the below note in for people that find this PR --> > [!NOTE] > Are you waiting for the changes in this PR to be merged? > It would be very helpful if you could [test the resulting artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from this PR and let us know in a comment if this change resolves your issue. Thank you! ### Issue Details Clearing an attached VisualStateGroupList removes groups without detaching the state triggers nested inside them. A trigger subscribed to a shared managed publisher in OnAttached() therefore stays subscribed and retained after its group is removed. ### Root Cause An attached `StateTriggerBase` runs `OnAttached()`, where custom triggers may subscribe to long-lived publishers such as static events. They rely on `SendDetached()` invoking `OnDetached()` to unsubscribe. Replacing the entire `VisualStateGroups` attached property already performed this teardown. However, mutating the existing `VisualStateGroupList` did not: - `Clear` - `Remove` - `RemoveAt` - Adding a group that replaces one with the same name - Replacing a group through the indexer These paths only removed `StatesChanged` handlers and collection entries. Nested triggers remained attached, leaving retention paths such as: ```text static publisher → event delegate → state trigger → page-local data ``` ### Description of Change Added a shared `DetachGroup` teardown method that: 1. Unsubscribes the group's `StatesChanged` handler. 2. Calls `SendDetached()` on every nested state trigger. 3. Removes the group's reference to its associated visual element. All removal and replacement paths in `VisualStateGroupList` now use this teardown. The `Remove` implementation also identifies the actual stored group before removing it. This ensures the correct instance is detached even when another equality-equivalent object is supplied. ## Regression coverage Added tests to `VisualStateManagerTests.cs` verifying trigger detachment for: - `Clear` - `Remove` - `RemoveAt` - Duplicate-name replacement through `Add` - Index replacement ### Issues Fixed Fixes #38245 **Tested the behavior in the following platforms.** - [x] Android - [x] Windows - [x] iOS - [x] Mac | Before | After | |---------|--------| | <img width="300" height="600" alt="image" src="https://github.com/user-attachments/assets/3f5ede16-f0ee-4aac-b728-07835001f851" /> | <img width="300" height="600" alt="image" src="https://github.com/user-attachments/assets/dafc0ce1-e9b4-439d-949e-cbe29a29af49" /> | --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…ggers attached (#38439) <!-- Please let the below note in for people that find this PR --> > [!NOTE] > Are you waiting for the changes in this PR to be merged? > It would be very helpful if you could [test the resulting artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from this PR and let us know in a comment if this change resolves your issue. Thank you! ### Issue Details Clearing an attached VisualStateGroupList removes groups without detaching the state triggers nested inside them. A trigger subscribed to a shared managed publisher in OnAttached() therefore stays subscribed and retained after its group is removed. ### Root Cause An attached `StateTriggerBase` runs `OnAttached()`, where custom triggers may subscribe to long-lived publishers such as static events. They rely on `SendDetached()` invoking `OnDetached()` to unsubscribe. Replacing the entire `VisualStateGroups` attached property already performed this teardown. However, mutating the existing `VisualStateGroupList` did not: - `Clear` - `Remove` - `RemoveAt` - Adding a group that replaces one with the same name - Replacing a group through the indexer These paths only removed `StatesChanged` handlers and collection entries. Nested triggers remained attached, leaving retention paths such as: ```text static publisher → event delegate → state trigger → page-local data ``` ### Description of Change Added a shared `DetachGroup` teardown method that: 1. Unsubscribes the group's `StatesChanged` handler. 2. Calls `SendDetached()` on every nested state trigger. 3. Removes the group's reference to its associated visual element. All removal and replacement paths in `VisualStateGroupList` now use this teardown. The `Remove` implementation also identifies the actual stored group before removing it. This ensures the correct instance is detached even when another equality-equivalent object is supplied. ## Regression coverage Added tests to `VisualStateManagerTests.cs` verifying trigger detachment for: - `Clear` - `Remove` - `RemoveAt` - Duplicate-name replacement through `Add` - Index replacement ### Issues Fixed Fixes #38245 **Tested the behavior in the following platforms.** - [x] Android - [x] Windows - [x] iOS - [x] Mac | Before | After | |---------|--------| | <img width="300" height="600" alt="image" src="https://github.com/user-attachments/assets/3f5ede16-f0ee-4aac-b728-07835001f851" /> | <img width="300" height="600" alt="image" src="https://github.com/user-attachments/assets/dafc0ce1-e9b4-439d-949e-cbe29a29af49" /> | --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…ggers attached (#38439) <!-- Please let the below note in for people that find this PR --> > [!NOTE] > Are you waiting for the changes in this PR to be merged? > It would be very helpful if you could [test the resulting artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from this PR and let us know in a comment if this change resolves your issue. Thank you! ### Issue Details Clearing an attached VisualStateGroupList removes groups without detaching the state triggers nested inside them. A trigger subscribed to a shared managed publisher in OnAttached() therefore stays subscribed and retained after its group is removed. ### Root Cause An attached `StateTriggerBase` runs `OnAttached()`, where custom triggers may subscribe to long-lived publishers such as static events. They rely on `SendDetached()` invoking `OnDetached()` to unsubscribe. Replacing the entire `VisualStateGroups` attached property already performed this teardown. However, mutating the existing `VisualStateGroupList` did not: - `Clear` - `Remove` - `RemoveAt` - Adding a group that replaces one with the same name - Replacing a group through the indexer These paths only removed `StatesChanged` handlers and collection entries. Nested triggers remained attached, leaving retention paths such as: ```text static publisher → event delegate → state trigger → page-local data ``` ### Description of Change Added a shared `DetachGroup` teardown method that: 1. Unsubscribes the group's `StatesChanged` handler. 2. Calls `SendDetached()` on every nested state trigger. 3. Removes the group's reference to its associated visual element. All removal and replacement paths in `VisualStateGroupList` now use this teardown. The `Remove` implementation also identifies the actual stored group before removing it. This ensures the correct instance is detached even when another equality-equivalent object is supplied. ## Regression coverage Added tests to `VisualStateManagerTests.cs` verifying trigger detachment for: - `Clear` - `Remove` - `RemoveAt` - Duplicate-name replacement through `Add` - Index replacement ### Issues Fixed Fixes #38245 **Tested the behavior in the following platforms.** - [x] Android - [x] Windows - [x] iOS - [x] Mac | Before | After | |---------|--------| | <img width="300" height="600" alt="image" src="https://github.com/user-attachments/assets/3f5ede16-f0ee-4aac-b728-07835001f851" /> | <img width="300" height="600" alt="image" src="https://github.com/user-attachments/assets/dafc0ce1-e9b4-439d-949e-cbe29a29af49" /> | --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…ggers attached (#38439) <!-- Please let the below note in for people that find this PR --> > [!NOTE] > Are you waiting for the changes in this PR to be merged? > It would be very helpful if you could [test the resulting artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from this PR and let us know in a comment if this change resolves your issue. Thank you! ### Issue Details Clearing an attached VisualStateGroupList removes groups without detaching the state triggers nested inside them. A trigger subscribed to a shared managed publisher in OnAttached() therefore stays subscribed and retained after its group is removed. ### Root Cause An attached `StateTriggerBase` runs `OnAttached()`, where custom triggers may subscribe to long-lived publishers such as static events. They rely on `SendDetached()` invoking `OnDetached()` to unsubscribe. Replacing the entire `VisualStateGroups` attached property already performed this teardown. However, mutating the existing `VisualStateGroupList` did not: - `Clear` - `Remove` - `RemoveAt` - Adding a group that replaces one with the same name - Replacing a group through the indexer These paths only removed `StatesChanged` handlers and collection entries. Nested triggers remained attached, leaving retention paths such as: ```text static publisher → event delegate → state trigger → page-local data ``` ### Description of Change Added a shared `DetachGroup` teardown method that: 1. Unsubscribes the group's `StatesChanged` handler. 2. Calls `SendDetached()` on every nested state trigger. 3. Removes the group's reference to its associated visual element. All removal and replacement paths in `VisualStateGroupList` now use this teardown. The `Remove` implementation also identifies the actual stored group before removing it. This ensures the correct instance is detached even when another equality-equivalent object is supplied. ## Regression coverage Added tests to `VisualStateManagerTests.cs` verifying trigger detachment for: - `Clear` - `Remove` - `RemoveAt` - Duplicate-name replacement through `Add` - Index replacement ### Issues Fixed Fixes #38245 **Tested the behavior in the following platforms.** - [x] Android - [x] Windows - [x] iOS - [x] Mac | Before | After | |---------|--------| | <img width="300" height="600" alt="image" src="https://github.com/user-attachments/assets/3f5ede16-f0ee-4aac-b728-07835001f851" /> | <img width="300" height="600" alt="image" src="https://github.com/user-attachments/assets/dafc0ce1-e9b4-439d-949e-cbe29a29af49" /> | --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…ggers attached (#38439) <!-- Please let the below note in for people that find this PR --> > [!NOTE] > Are you waiting for the changes in this PR to be merged? > It would be very helpful if you could [test the resulting artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from this PR and let us know in a comment if this change resolves your issue. Thank you! ### Issue Details Clearing an attached VisualStateGroupList removes groups without detaching the state triggers nested inside them. A trigger subscribed to a shared managed publisher in OnAttached() therefore stays subscribed and retained after its group is removed. ### Root Cause An attached `StateTriggerBase` runs `OnAttached()`, where custom triggers may subscribe to long-lived publishers such as static events. They rely on `SendDetached()` invoking `OnDetached()` to unsubscribe. Replacing the entire `VisualStateGroups` attached property already performed this teardown. However, mutating the existing `VisualStateGroupList` did not: - `Clear` - `Remove` - `RemoveAt` - Adding a group that replaces one with the same name - Replacing a group through the indexer These paths only removed `StatesChanged` handlers and collection entries. Nested triggers remained attached, leaving retention paths such as: ```text static publisher → event delegate → state trigger → page-local data ``` ### Description of Change Added a shared `DetachGroup` teardown method that: 1. Unsubscribes the group's `StatesChanged` handler. 2. Calls `SendDetached()` on every nested state trigger. 3. Removes the group's reference to its associated visual element. All removal and replacement paths in `VisualStateGroupList` now use this teardown. The `Remove` implementation also identifies the actual stored group before removing it. This ensures the correct instance is detached even when another equality-equivalent object is supplied. ## Regression coverage Added tests to `VisualStateManagerTests.cs` verifying trigger detachment for: - `Clear` - `Remove` - `RemoveAt` - Duplicate-name replacement through `Add` - Index replacement ### Issues Fixed Fixes #38245 **Tested the behavior in the following platforms.** - [x] Android - [x] Windows - [x] iOS - [x] Mac | Before | After | |---------|--------| | <img width="300" height="600" alt="image" src="https://github.com/user-attachments/assets/3f5ede16-f0ee-4aac-b728-07835001f851" /> | <img width="300" height="600" alt="image" src="https://github.com/user-attachments/assets/dafc0ce1-e9b4-439d-949e-cbe29a29af49" /> | --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>


Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Issue Details
Clearing an attached VisualStateGroupList removes groups without detaching the state triggers nested inside them. A trigger subscribed to a shared managed publisher in OnAttached() therefore stays subscribed and retained after its group is removed.
Root Cause
An attached
StateTriggerBaserunsOnAttached(), where custom triggers may subscribe to long-lived publishers such as static events. They rely onSendDetached()invokingOnDetached()to unsubscribe.Replacing the entire
VisualStateGroupsattached property already performed this teardown. However, mutating the existingVisualStateGroupListdid not:ClearRemoveRemoveAtThese paths only removed
StatesChangedhandlers and collection entries. Nested triggers remained attached, leaving retention paths such as:Description of Change
Added a shared
DetachGroupteardown method that:StatesChangedhandler.SendDetached()on every nested state trigger.All removal and replacement paths in
VisualStateGroupListnow use this teardown.The
Removeimplementation also identifies the actual stored group before removing it. This ensures the correct instance is detached even when another equality-equivalent object is supplied.Regression coverage
Added tests to
VisualStateManagerTests.csverifying trigger detachment for:ClearRemoveRemoveAtAddIssues Fixed
Fixes #38245
Tested the behavior in the following platforms.