Repository navigation
Fix memory leak when removing VisualState state triggers - #38444
Conversation
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 38444Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 38444" |
|
Hey there @@Shalini-Ashokan! 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.
|
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.
MauiBot
left a comment
There was a problem hiding this comment.
Expert Review — 1 findings
See inline comments for details.
MauiBot
left a comment
There was a problem hiding this comment.
AI Review Summary
@Shalini-Ashokan — new AI review results are available based on commit
af3847d.
🗂️ Review Sessions — click to expand
🚦 Gate — Test Before & After Fix
Gate Result: ❌ FAILED
Platform: ANDROID · Base: main · Merge base: 1cd2e15b
🩺 Fix does not pass the tests — every test still fails after applying the fix. The PR's change does not resolve the failure(s).
| Test | Without Fix (expect FAIL) | With Fix (expect PASS) |
|---|---|---|
🧪 VisualStateManagerTests VisualStateManagerTests |
✅ FAIL — 108s | ❌ FAIL — 120s |
🔴 Without fix — 🧪 VisualStateManagerTests: FAIL ✅ · 108s
Error-relevant lines (filtered from the build log):
at Microsoft.Maui.Controls.Core.UnitTests.VisualStateManagerTests.RemovingStateTriggerDetachesIt(StateTriggerRemovalOperation operation) in /_/src/Controls/tests/Core.UnitTests/VisualStateManagerTests.cs:line 799
at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
at InvokeStub_VisualStateManagerTests.RemovingStateTriggerDetachesIt(Object, Span`1)
at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
🟢 With fix — 🧪 VisualStateManagerTests: FAIL ❌ · 120s
Error-relevant lines (filtered from the build log):
at Microsoft.Maui.Controls.Core.UnitTests.VisualStateManagerTests.RemovingStateTriggerDetachesIt(StateTriggerRemovalOperation operation) in /_/src/Controls/tests/Core.UnitTests/VisualStateManagerTests.cs:line 799
at InvokeStub_VisualStateManagerTests.RemovingStateTriggerDetachesIt(Object, Span`1)
at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
⚠️ Failure Details
- ❌ VisualStateManagerTests FAILED with fix (should pass)
Device tests: 1 of 32 failed
📁 Fix files reverted (1 files)
src/Controls/src/Core/VisualStateManager.cs
📋 Pre-Flight — Context & Validation
PR #38444 Pre-Flight
Context
- Title: Fix memory leak when removing VisualState state triggers
- Issue: #38243,
[leak-scan] VisualState.StateTriggers — removal skips trigger detachment - Base / materialized head:
1cd2e15ba4/68ffc497f1 - Platform: Android (the affected collection/lifecycle code is cross-platform managed code)
- Problem: Removing an attached
StateTriggerBasefromVisualState.StateTriggersdoes not pair the framework-issuedSendAttached()withSendDetached(). A trigger subscribed to a long-lived publisher can therefore retain itself and page-local objects. - Retention path: shared publisher -> event delegate -> removed trigger -> page-local payload.
Existing PR Approach
The PR extends WatchAddList<T> with an optional removal callback. VisualState.OnStateTriggersRemoved calls SendDetached(), clears VisualState, and recalculates active triggers. Clear, Remove, and RemoveAt invoke the removal callback.
The indexer setter still performs only:
set => _internalList[index] = value;Consequently, replacement neither detaches/clears the old trigger nor assigns the new trigger to its VisualState. The source diff changes only:
src/Controls/src/Core/VisualStateManager.cs(production fix)src/Controls/tests/Core.UnitTests/VisualStateManagerTests.cs(regression theory)
The baseline script's fix-file boundary is expected to allow modification only of src/Controls/src/Core/VisualStateManager.cs; the committed test file must remain unchanged during try-fix attempts.
Gate Evidence
The completed gate must not be rerun. It established:
- Without the production fix, all four
RemovingStateTriggerDetachesItcases fail. - With the PR fix,
Clear,Remove, andRemoveAtpass. Replaceconsistently fails atAssert.False(trigger.IsAttached): expectedfalse, actualtrue.- With-fix class-filter result: 33 total, 31 passed, 1 failed (
Replace), 1 skipped.
Gate verdict: FAILED — definitive functional failure, not an environment blocker.
Bounded Validation
Primary and regression scope are the committed VisualStateManagerTests class (including all four removal operations):
dotnet test src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj --filter "FullyQualifiedName~VisualStateManagerTests"No additional mandatory regression tests were listed for STEP 5a. Do not run the gate or any full suite.
Alternative-Fix Requirements
Each attempt must establish the broken baseline using the required script, implement one mechanism-level alternative to the PR and prior attempts, run only the bounded class-filter test above, capture all try-fix artifacts and inline expert self-review, then restore with:
pwsh .github/scripts/EstablishBrokenBaseline.ps1 -RestoreCandidate 1 must not merely reapply the PR's incomplete per-operation callback. Candidate 2 must consume candidate 1's recorded result and choose a distinct root-cause mechanism.
🔬 Code Review — Deep Analysis
Expert Code Review — PR #38444 (raw submitted fix)
Independent Assessment (code-first)
The submitted change adds removal notifications to WatchAddList<T> and wires VisualState.StateTriggers removals to OnStateTriggersRemoved, which calls SendDetached(), nulls VisualState, and recomputes active triggers. This correctly covers explicit removal APIs (Clear, Remove, RemoveAt) but does not fully cover replacement semantics.
Concrete Findings
src/Controls/src/Core/VisualStateManager.cs:875
[critical] Logic and Correctness Verification — the indexer setter remains direct assignment (set => _internalList[index] = value;). Replacing an existing trigger (state.StateTriggers[0] = newTrigger) does not invoke removal lifecycle for the old trigger, soSendDetached()is skipped and old subscriptions can remain alive.
Failure-Mode Analysis
- Trigger path: attached trigger subscribes to a long-lived publisher in
OnAttached(). - Mutation: caller replaces trigger via
state.StateTriggers[index] = .... - Expected behavior: old trigger is detached exactly once and disassociated from its state.
- Actual behavior in submitted fix: old trigger is overwritten without removal callback, preserving the leak mechanism for replace operations.
- This is corroborated by the focused regression case
RemovingStateTriggerDetachesIt(operation: Replace)in gate evidence.
Reconciliation with Narrative and Prior Review
- PR narrative claims replacement handling is included; current submitted code does not implement replacement detachment in
WatchAddList<T>.this[index].set. - Prior top-level review exists (
CHANGES_REQUESTEDfrom MauiBot). Inline review comment surface for this fetch was empty, and issue comments were workflow/bot traffic.
Gate / Validation Caveat
- Trusted Gate is already known FAILED for this raw submitted fix and was not rerun (per instruction).
- Gate evidence indicates
Clear,Remove, andRemoveAtpaths pass whileReplaceremains failing. gh pr checks --requiredcould not be retrieved in this environment due unauthenticatedgh; required-check status from GitHub is therefore undetermined here.
Verdict
NEEDS_CHANGES
Confidence
low (high confidence in the specific defect, but overall recommendation confidence capped by unavailable required-check query in this environment).
🛠️ Try-Fix — Analysis & Comparison
PR #38444 Try-Fix Aggregate
Candidate 1 — Replace-Aware Trigger Lifecycle
- Result: Blocked
- Approach: Make index replacement detach/clear the outgoing trigger before assigning/associating the incoming trigger and recalculating state.
- Difference from PR: Handles replacement as an explicit old-to-new lifecycle transaction rather than depending on the PR's removal-only callback path, which the indexer bypasses.
- Implementation/test: None.
EstablishBrokenBaseline.ps1rejected the pre-existing dirty worktree and created no baseline state or edit allow-list. - Diff: Empty.
- Self-review: 0 findings.
- Restoration: Exact
-Restorecommand ran; expected no-state result (Restored: False) because no baseline was created and no attempt edits occurred. - Artifacts:
CustomAgentLogsTmp/PRState/38444/PRAgent/try-fix/attempt-1
Full narrative: CustomAgentLogsTmp/PRState/38444/PRAgent/try-fix-1/content.md.
Candidate 2 — Snapshot-Reconciled Collection Changes
- Result: Blocked
- Approach: Route every collection mutation through one notification, then reconcile a retained prior snapshot against current triggers to derive removals and additions.
- Difference from PR and Candidate 1: Avoids both per-mutator removal callbacks and a replacement-specific old-to-new transaction. The same post-mutation reconciliation mechanism covers clear, remove, remove-at, and replacement.
- Implementation/test: None. The mandatory baseline script again rejected the pre-existing dirty worktree and produced no edit allow-list.
- Diff: Empty.
- Self-review: 0 findings.
- Restoration: Exact
-Restorecommand ran; expected no-state result (Restored: False) because no baseline was created and no attempt edits occurred. - Artifacts:
CustomAgentLogsTmp/PRState/38444/PRAgent/try-fix/attempt-2
Full narrative: CustomAgentLogsTmp/PRState/38444/PRAgent/try-fix-2/content.md.
Aggregate Outcome
Two candidates were generated, in the required model order, and no further agents were launched. Both are distinct from the PR and from each other, but neither could be implemented or empirically tested because EstablishBrokenBaseline.ps1 rejected the shared pre-existing dirty worktree before creating a safe edit/restoration boundary.
| Candidate | Mechanism | Result | Test Runs | Diff |
|---|---|---|---|---|
| 1 | Replacement-specific trigger lifecycle transaction | Blocked | 0 | Empty |
| 2 | Universal mutation notification with snapshot reconciliation | Blocked | 0 | Empty |
The completed gate was not rerun, gate/content.md was not modified, no full suite ran, and no production or committed test file was changed.
📝 PR Finalize — Recommended Title & Description
Assessment: ✏️ Recommend updating — the current title is not component-scoped and the description incorrectly claims index replacement is handled even though the submitted indexer still bypasses removal callbacks and its replacement regression fails.
Recommended title
[All] VisualState: Detach triggers on Clear, Remove, and RemoveAt
Recommended description
### Issue Details
Removing a `StateTriggerBase` from `VisualState.StateTriggers` does not detach the trigger. If it subscribed to a long-lived event, that subscription can retain the trigger and its referenced objects.
For example, calling `state.StateTriggers.Clear()` previously removed the trigger from the collection without invoking its detach lifecycle.
### Root Cause
`VisualState.StateTriggers` uses `WatchAddList<StateTriggerBase>`, which previously notified only when triggers were added. `Clear()`, `Remove()`, and `RemoveAt()` therefore removed triggers without calling `SendDetached()`.
The indexer setter still assigns directly to the backing list. At the submitted PR HEAD, index replacement does not invoke the new removal callback and remains an unresolved removal path.
### Description of Change
Updated `WatchAddList<T>` to support an optional removal callback and invoke it from `Clear()`, `Remove()`, and `RemoveAt()`. `VisualState` uses that callback to call `SendDetached()` for removed triggers, clear their `VisualState` references, and request active-trigger recalculation.
Added `RemovingStateTriggerDetachesIt` regression coverage for `Clear()`, `Remove()`, `RemoveAt()`, and index replacement. The first three cases are covered by the submitted implementation; the replacement case currently exposes the unresolved indexer gap and fails at the submitted PR HEAD.
### Platforms Tested
- [x] Android
- [x] Windows
- [x] iOS
- [x] Mac
The lifecycle implementation is shared managed code across these platforms.
### Issues Fixed
Fixes #38243
### Before and After
| Before | After |
|---|---|
| <img width="364" height="733" alt="Before removing the state trigger" src="https://github.com/user-attachments/assets/e2287141-a2b5-4791-970c-a5301b266b6b" /> | <img width="364" height="733" alt="After removing the state trigger" src="https://github.com/user-attachments/assets/44aa8172-94ae-4698-96bd-2959981ccb4a" /> |
🏁 Report — Final Recommendation
⚠️ Final Recommendation: REQUEST CHANGES
Winner
pr-plus-reviewer is the single winning candidate. It closes the submitted fix's replacement-path lifecycle gap with a localized indexer transaction and passes the required VisualStateManagerTests scope: 32 passed, 0 failed, 1 skipped (33 total).
Comparative Ranking
| Rank | Candidate | Implementation | Regression evidence | Assessment |
|---|---|---|---|---|
| 1 | pr-plus-reviewer |
Replaces the backing-list item, notifies removal for the outgoing trigger, then runs add-side association for the incoming trigger; same-instance assignment remains a no-op. | Passed: 32 passed, 0 failed, 1 skipped. | Best combination of correctness, locality, and empirical evidence. |
| 2 | try-fix-1 |
Proposed a replacement-specific old-to-new lifecycle transaction. | Blocked: no implementation, diff, or test run because baseline establishment rejected the dirty raw worktree. | Mechanically aligned with the winning approach, but not a usable or verified candidate. |
| 3 | try-fix-2 |
Proposed universal mutation notification plus snapshot reconciliation. | Blocked: no implementation, diff, or test run for the same baseline blocker. | Could cover all mutation forms, but adds state and reconciliation complexity without empirical evidence. |
| 4 | pr |
Adds removal callbacks for Clear, Remove, and RemoveAt, but leaves the indexer setter as direct assignment. |
Failed trusted Gate: Replace failed; with-fix result was 31 passed, 1 failed, 1 skipped. |
The submitted PR still leaks the outgoing trigger on replacement and contradicts its claimed replacement coverage. |
Expert Review Reconciliation
The one expert pass found a critical correctness defect at src/Controls/src/Core/VisualStateManager.cs:875: StateTriggers[index] = replacement bypasses OnStateTriggersRemoved, so the outgoing trigger is not detached and can remain retained by a long-lived event publisher. The trusted Gate independently reproduced this defect in RemovingStateTriggerDetachesIt(Replace).
The consolidated candidate addresses both sides of replacement semantics. Removal notification detaches and disassociates the outgoing trigger, while add notification associates the incoming trigger using the existing WatchAddList<T> callback contract. Avoiding callbacks for same-instance assignment prevents detaching an object that remains in the collection.
Validation and Uncertainty
- The trusted Gate was not rerun.
- The raw
prcandidate has definitive failing regression evidence and therefore ranks below the passing candidate. - Both
try-fix-*entries are conceptual only; neither produced a diff or validation result. pr-plus-reviewerran only the explicitly required targeted class filter, once, in the prescribed sandbox. No full suite was run.- Required GitHub check status remained unavailable to the expert reviewer, so this recommendation is based on the trusted Gate and focused local candidate validation rather than complete CI.
Required Change
Apply the reviewer patch represented by pr-plus-reviewer/reviewer.patch to the submitted PR before merge.
📱 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 submitted PR fails its replacement regression because WatchAddList<T>'s indexer bypasses removal and add callbacks. pr-plus-reviewer fixes that lifecycle transaction and passes the required VisualStateManagerTests scope with 32 passed, 0 failed, and 1 skipped.
Address the actionable findings in this review before merging.
@kubaflo, I have addressed the AI concerns |
There was a problem hiding this comment.
🟡 Changes recommended
The current implementation still misses correct behavior for Clear() ordering and index replacement (indexer setter) removal notification, so the leak scenario is not fully fixed as described and tested.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
This PR aims to fix a memory leak in VisualState.StateTriggers by ensuring removed StateTriggerBase instances are properly detached (calling SendDetached()), clearing their VisualState reference, and recalculating active triggers so long-lived event subscriptions don’t retain removed triggers.
Changes:
- Added removal notifications to
WatchAddList<T>and wiredVisualState.StateTriggersto detach triggers on removal. - Added unit test coverage for trigger detachment when the collection is mutated (Clear/Remove/RemoveAt).
File summaries
| File | Description |
|---|---|
| src/Controls/src/Core/VisualStateManager.cs | Adds removal callback support to WatchAddList<T> and detaches removed state triggers in VisualState. |
| src/Controls/tests/Core.UnitTests/VisualStateManagerTests.cs | Adds a regression test validating that removed triggers detach and clear their VisualState. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 3
- Review effort level: Lite
| public void Clear() | ||
| { | ||
| if (_internalList.Count == 0) | ||
| return; | ||
|
|
||
| _onRemove?.Invoke(_internalList); | ||
| _internalList.Clear(); | ||
|
|
||
| } |
| public void RemoveAt(int index) | ||
| { | ||
| var removedItem = _internalList[index]; | ||
| _internalList.RemoveAt(index); | ||
| _onRemove?.Invoke(new List<T> { removedItem }); |
| [Theory] | ||
| [InlineData(StateTriggerRemovalOperation.Clear)] | ||
| [InlineData(StateTriggerRemovalOperation.Remove)] | ||
| [InlineData(StateTriggerRemovalOperation.RemoveAt)] | ||
| public void RemovingStateTriggerDetachesIt(StateTriggerRemovalOperation operation) |
This comment has been minimized.
This comment has been minimized.
|
Warning Your |
<!-- 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! <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issue Details Removing a StateTriggerBase from VisualState.StateTriggers does not detach the trigger. If it subscribed to a long-lived event, the subscription can retain the trigger and its referenced objects. For example, calling state.StateTriggers.Clear() removes the trigger from the collection without calling its detach lifecycle. ### Root Cause VisualState.StateTriggers uses WatchAddList<StateTriggerBase>, which previously notified only when triggers were added. Operations such as Clear(), Remove(), RemoveAt(), and replacement therefore removed triggers without calling SendDetached(). ### Description of Change Updated WatchAddList<T> to support an optional removal callback and added removal handling to VisualState. Removed triggers are detached, their VisualState references are cleared, and active triggers are recalculated. Added regression coverage for Clear(), Remove(), RemoveAt(), and index replacement to verify that each removed trigger is detached exactly once. Validated the behavior in the following platforms - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Issues Fixed Fixes #38243 ### Output ScreenShot |Before|After| |--|--| | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/e2287141-a2b5-4791-970c-a5301b266b6b" /> | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/44aa8172-94ae-4698-96bd-2959981ccb4a" /> |
<!-- 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! <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issue Details Removing a StateTriggerBase from VisualState.StateTriggers does not detach the trigger. If it subscribed to a long-lived event, the subscription can retain the trigger and its referenced objects. For example, calling state.StateTriggers.Clear() removes the trigger from the collection without calling its detach lifecycle. ### Root Cause VisualState.StateTriggers uses WatchAddList<StateTriggerBase>, which previously notified only when triggers were added. Operations such as Clear(), Remove(), RemoveAt(), and replacement therefore removed triggers without calling SendDetached(). ### Description of Change Updated WatchAddList<T> to support an optional removal callback and added removal handling to VisualState. Removed triggers are detached, their VisualState references are cleared, and active triggers are recalculated. Added regression coverage for Clear(), Remove(), RemoveAt(), and index replacement to verify that each removed trigger is detached exactly once. Validated the behavior in the following platforms - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Issues Fixed Fixes #38243 ### Output ScreenShot |Before|After| |--|--| | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/e2287141-a2b5-4791-970c-a5301b266b6b" /> | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/44aa8172-94ae-4698-96bd-2959981ccb4a" /> |
<!-- 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! <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issue Details Removing a StateTriggerBase from VisualState.StateTriggers does not detach the trigger. If it subscribed to a long-lived event, the subscription can retain the trigger and its referenced objects. For example, calling state.StateTriggers.Clear() removes the trigger from the collection without calling its detach lifecycle. ### Root Cause VisualState.StateTriggers uses WatchAddList<StateTriggerBase>, which previously notified only when triggers were added. Operations such as Clear(), Remove(), RemoveAt(), and replacement therefore removed triggers without calling SendDetached(). ### Description of Change Updated WatchAddList<T> to support an optional removal callback and added removal handling to VisualState. Removed triggers are detached, their VisualState references are cleared, and active triggers are recalculated. Added regression coverage for Clear(), Remove(), RemoveAt(), and index replacement to verify that each removed trigger is detached exactly once. Validated the behavior in the following platforms - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Issues Fixed Fixes #38243 ### Output ScreenShot |Before|After| |--|--| | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/e2287141-a2b5-4791-970c-a5301b266b6b" /> | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/44aa8172-94ae-4698-96bd-2959981ccb4a" /> |
<!-- 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! <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issue Details Removing a StateTriggerBase from VisualState.StateTriggers does not detach the trigger. If it subscribed to a long-lived event, the subscription can retain the trigger and its referenced objects. For example, calling state.StateTriggers.Clear() removes the trigger from the collection without calling its detach lifecycle. ### Root Cause VisualState.StateTriggers uses WatchAddList<StateTriggerBase>, which previously notified only when triggers were added. Operations such as Clear(), Remove(), RemoveAt(), and replacement therefore removed triggers without calling SendDetached(). ### Description of Change Updated WatchAddList<T> to support an optional removal callback and added removal handling to VisualState. Removed triggers are detached, their VisualState references are cleared, and active triggers are recalculated. Added regression coverage for Clear(), Remove(), RemoveAt(), and index replacement to verify that each removed trigger is detached exactly once. Validated the behavior in the following platforms - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Issues Fixed Fixes #38243 ### Output ScreenShot |Before|After| |--|--| | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/e2287141-a2b5-4791-970c-a5301b266b6b" /> | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/44aa8172-94ae-4698-96bd-2959981ccb4a" /> |
<!-- 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! <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issue Details Removing a StateTriggerBase from VisualState.StateTriggers does not detach the trigger. If it subscribed to a long-lived event, the subscription can retain the trigger and its referenced objects. For example, calling state.StateTriggers.Clear() removes the trigger from the collection without calling its detach lifecycle. ### Root Cause VisualState.StateTriggers uses WatchAddList<StateTriggerBase>, which previously notified only when triggers were added. Operations such as Clear(), Remove(), RemoveAt(), and replacement therefore removed triggers without calling SendDetached(). ### Description of Change Updated WatchAddList<T> to support an optional removal callback and added removal handling to VisualState. Removed triggers are detached, their VisualState references are cleared, and active triggers are recalculated. Added regression coverage for Clear(), Remove(), RemoveAt(), and index replacement to verify that each removed trigger is detached exactly once. Validated the behavior in the following platforms - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Issues Fixed Fixes #38243 ### Output ScreenShot |Before|After| |--|--| | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/e2287141-a2b5-4791-970c-a5301b266b6b" /> | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/44aa8172-94ae-4698-96bd-2959981ccb4a" /> |
<!-- 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! <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issue Details Removing a StateTriggerBase from VisualState.StateTriggers does not detach the trigger. If it subscribed to a long-lived event, the subscription can retain the trigger and its referenced objects. For example, calling state.StateTriggers.Clear() removes the trigger from the collection without calling its detach lifecycle. ### Root Cause VisualState.StateTriggers uses WatchAddList<StateTriggerBase>, which previously notified only when triggers were added. Operations such as Clear(), Remove(), RemoveAt(), and replacement therefore removed triggers without calling SendDetached(). ### Description of Change Updated WatchAddList<T> to support an optional removal callback and added removal handling to VisualState. Removed triggers are detached, their VisualState references are cleared, and active triggers are recalculated. Added regression coverage for Clear(), Remove(), RemoveAt(), and index replacement to verify that each removed trigger is detached exactly once. Validated the behavior in the following platforms - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Issues Fixed Fixes #38243 ### Output ScreenShot |Before|After| |--|--| | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/e2287141-a2b5-4791-970c-a5301b266b6b" /> | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/44aa8172-94ae-4698-96bd-2959981ccb4a" /> |
<!-- 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! <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issue Details Removing a StateTriggerBase from VisualState.StateTriggers does not detach the trigger. If it subscribed to a long-lived event, the subscription can retain the trigger and its referenced objects. For example, calling state.StateTriggers.Clear() removes the trigger from the collection without calling its detach lifecycle. ### Root Cause VisualState.StateTriggers uses WatchAddList<StateTriggerBase>, which previously notified only when triggers were added. Operations such as Clear(), Remove(), RemoveAt(), and replacement therefore removed triggers without calling SendDetached(). ### Description of Change Updated WatchAddList<T> to support an optional removal callback and added removal handling to VisualState. Removed triggers are detached, their VisualState references are cleared, and active triggers are recalculated. Added regression coverage for Clear(), Remove(), RemoveAt(), and index replacement to verify that each removed trigger is detached exactly once. Validated the behavior in the following platforms - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Issues Fixed Fixes #38243 ### Output ScreenShot |Before|After| |--|--| | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/e2287141-a2b5-4791-970c-a5301b266b6b" /> | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/44aa8172-94ae-4698-96bd-2959981ccb4a" /> |
<!-- 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! <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issue Details Removing a StateTriggerBase from VisualState.StateTriggers does not detach the trigger. If it subscribed to a long-lived event, the subscription can retain the trigger and its referenced objects. For example, calling state.StateTriggers.Clear() removes the trigger from the collection without calling its detach lifecycle. ### Root Cause VisualState.StateTriggers uses WatchAddList<StateTriggerBase>, which previously notified only when triggers were added. Operations such as Clear(), Remove(), RemoveAt(), and replacement therefore removed triggers without calling SendDetached(). ### Description of Change Updated WatchAddList<T> to support an optional removal callback and added removal handling to VisualState. Removed triggers are detached, their VisualState references are cleared, and active triggers are recalculated. Added regression coverage for Clear(), Remove(), RemoveAt(), and index replacement to verify that each removed trigger is detached exactly once. Validated the behavior in the following platforms - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Issues Fixed Fixes #38243 ### Output ScreenShot |Before|After| |--|--| | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/e2287141-a2b5-4791-970c-a5301b266b6b" /> | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/44aa8172-94ae-4698-96bd-2959981ccb4a" /> |
<!-- 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! <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issue Details Removing a StateTriggerBase from VisualState.StateTriggers does not detach the trigger. If it subscribed to a long-lived event, the subscription can retain the trigger and its referenced objects. For example, calling state.StateTriggers.Clear() removes the trigger from the collection without calling its detach lifecycle. ### Root Cause VisualState.StateTriggers uses WatchAddList<StateTriggerBase>, which previously notified only when triggers were added. Operations such as Clear(), Remove(), RemoveAt(), and replacement therefore removed triggers without calling SendDetached(). ### Description of Change Updated WatchAddList<T> to support an optional removal callback and added removal handling to VisualState. Removed triggers are detached, their VisualState references are cleared, and active triggers are recalculated. Added regression coverage for Clear(), Remove(), RemoveAt(), and index replacement to verify that each removed trigger is detached exactly once. Validated the behavior in the following platforms - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Issues Fixed Fixes #38243 ### Output ScreenShot |Before|After| |--|--| | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/e2287141-a2b5-4791-970c-a5301b266b6b" /> | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/44aa8172-94ae-4698-96bd-2959981ccb4a" /> |
<!-- 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! <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issue Details Removing a StateTriggerBase from VisualState.StateTriggers does not detach the trigger. If it subscribed to a long-lived event, the subscription can retain the trigger and its referenced objects. For example, calling state.StateTriggers.Clear() removes the trigger from the collection without calling its detach lifecycle. ### Root Cause VisualState.StateTriggers uses WatchAddList<StateTriggerBase>, which previously notified only when triggers were added. Operations such as Clear(), Remove(), RemoveAt(), and replacement therefore removed triggers without calling SendDetached(). ### Description of Change Updated WatchAddList<T> to support an optional removal callback and added removal handling to VisualState. Removed triggers are detached, their VisualState references are cleared, and active triggers are recalculated. Added regression coverage for Clear(), Remove(), RemoveAt(), and index replacement to verify that each removed trigger is detached exactly once. Validated the behavior in the following platforms - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Issues Fixed Fixes #38243 ### Output ScreenShot |Before|After| |--|--| | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/e2287141-a2b5-4791-970c-a5301b266b6b" /> | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/44aa8172-94ae-4698-96bd-2959981ccb4a" /> |
<!-- 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! <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issue Details Removing a StateTriggerBase from VisualState.StateTriggers does not detach the trigger. If it subscribed to a long-lived event, the subscription can retain the trigger and its referenced objects. For example, calling state.StateTriggers.Clear() removes the trigger from the collection without calling its detach lifecycle. ### Root Cause VisualState.StateTriggers uses WatchAddList<StateTriggerBase>, which previously notified only when triggers were added. Operations such as Clear(), Remove(), RemoveAt(), and replacement therefore removed triggers without calling SendDetached(). ### Description of Change Updated WatchAddList<T> to support an optional removal callback and added removal handling to VisualState. Removed triggers are detached, their VisualState references are cleared, and active triggers are recalculated. Added regression coverage for Clear(), Remove(), RemoveAt(), and index replacement to verify that each removed trigger is detached exactly once. Validated the behavior in the following platforms - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Issues Fixed Fixes #38243 ### Output ScreenShot |Before|After| |--|--| | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/e2287141-a2b5-4791-970c-a5301b266b6b" /> | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/44aa8172-94ae-4698-96bd-2959981ccb4a" /> |
<!-- 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! <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issue Details Removing a StateTriggerBase from VisualState.StateTriggers does not detach the trigger. If it subscribed to a long-lived event, the subscription can retain the trigger and its referenced objects. For example, calling state.StateTriggers.Clear() removes the trigger from the collection without calling its detach lifecycle. ### Root Cause VisualState.StateTriggers uses WatchAddList<StateTriggerBase>, which previously notified only when triggers were added. Operations such as Clear(), Remove(), RemoveAt(), and replacement therefore removed triggers without calling SendDetached(). ### Description of Change Updated WatchAddList<T> to support an optional removal callback and added removal handling to VisualState. Removed triggers are detached, their VisualState references are cleared, and active triggers are recalculated. Added regression coverage for Clear(), Remove(), RemoveAt(), and index replacement to verify that each removed trigger is detached exactly once. Validated the behavior in the following platforms - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Issues Fixed Fixes #38243 ### Output ScreenShot |Before|After| |--|--| | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/e2287141-a2b5-4791-970c-a5301b266b6b" /> | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/44aa8172-94ae-4698-96bd-2959981ccb4a" /> |
<!-- 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! <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issue Details Removing a StateTriggerBase from VisualState.StateTriggers does not detach the trigger. If it subscribed to a long-lived event, the subscription can retain the trigger and its referenced objects. For example, calling state.StateTriggers.Clear() removes the trigger from the collection without calling its detach lifecycle. ### Root Cause VisualState.StateTriggers uses WatchAddList<StateTriggerBase>, which previously notified only when triggers were added. Operations such as Clear(), Remove(), RemoveAt(), and replacement therefore removed triggers without calling SendDetached(). ### Description of Change Updated WatchAddList<T> to support an optional removal callback and added removal handling to VisualState. Removed triggers are detached, their VisualState references are cleared, and active triggers are recalculated. Added regression coverage for Clear(), Remove(), RemoveAt(), and index replacement to verify that each removed trigger is detached exactly once. Validated the behavior in the following platforms - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Issues Fixed Fixes #38243 ### Output ScreenShot |Before|After| |--|--| | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/e2287141-a2b5-4791-970c-a5301b266b6b" /> | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/44aa8172-94ae-4698-96bd-2959981ccb4a" /> |
<!-- 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! <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issue Details Removing a StateTriggerBase from VisualState.StateTriggers does not detach the trigger. If it subscribed to a long-lived event, the subscription can retain the trigger and its referenced objects. For example, calling state.StateTriggers.Clear() removes the trigger from the collection without calling its detach lifecycle. ### Root Cause VisualState.StateTriggers uses WatchAddList<StateTriggerBase>, which previously notified only when triggers were added. Operations such as Clear(), Remove(), RemoveAt(), and replacement therefore removed triggers without calling SendDetached(). ### Description of Change Updated WatchAddList<T> to support an optional removal callback and added removal handling to VisualState. Removed triggers are detached, their VisualState references are cleared, and active triggers are recalculated. Added regression coverage for Clear(), Remove(), RemoveAt(), and index replacement to verify that each removed trigger is detached exactly once. Validated the behavior in the following platforms - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Issues Fixed Fixes #38243 ### Output ScreenShot |Before|After| |--|--| | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/e2287141-a2b5-4791-970c-a5301b266b6b" /> | <img width="364" height="733" alt="image" src="https://github.com/user-attachments/assets/44aa8172-94ae-4698-96bd-2959981ccb4a" /> |
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
Removing a StateTriggerBase from VisualState.StateTriggers does not detach the trigger. If it subscribed to a long-lived event, the subscription can retain the trigger and its referenced objects.
For example, calling state.StateTriggers.Clear() removes the trigger from the collection without calling its detach lifecycle.
Root Cause
VisualState.StateTriggers uses WatchAddList, which previously notified only when triggers were added. Operations such as Clear(), Remove(), RemoveAt(), and replacement therefore removed triggers without calling SendDetached().
Description of Change
Updated WatchAddList to support an optional removal callback and added removal handling to VisualState. Removed triggers are detached, their VisualState references are cleared, and active triggers are recalculated.
Added regression coverage for Clear(), Remove(), RemoveAt(), and index replacement to verify that each removed trigger is detached exactly once.
Validated the behavior in the following platforms
Issues Fixed
Fixes #38243
Output ScreenShot