Repository navigation
[Windows] Fix Shell.Items.Clear() memory leak by disconnecting child handlers on removal - #36872
Conversation
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 36872Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 36872" |
|
Azure Pipelines: Successfully started running 1 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
|
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. |
|
/azp run maui-pr-uitests , maui-pr-devicetests |
|
Azure Pipelines: Successfully started running 2 pipeline(s). |
|
Azure Pipelines: Successfully started running 1 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Pull request overview
This PR addresses a Windows-specific memory leak when Shell.Items.Clear() removes the current ShellSection: the ShellSectionHandler gets reused for the next section, but the old section’s pages (and their children) can remain connected, retaining native resources. The fix proactively disconnects handlers for pages in the old section’s navigation stack when the old section is no longer attached to a Shell.
Changes:
- Windows: In
ShellSectionHandler.SetVirtualView, when switching sections, disconnect handlers for pages in the old section’s navigation stack if the old section is detached fromShell. - UI test: Enable the existing
Issue34898UI test to run on Windows by removing the#if TEST_FAILS_ON_WINDOWSguard. - HostApp metadata: Update
Issue34898’sPlatformAffectedmetadata (not used for filtering in HostApp discovery).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Controls/src/Core/Handlers/Shell/ShellSectionHandler.Windows.cs | Disconnects old section page handlers when the old section has been removed from the Shell hierarchy, preventing retained native resources. |
| src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue34898.cs | Removes a Windows skip-guard so the regression test executes on Windows. |
| src/Controls/tests/TestCases.HostApp/Issues/Issue34898.cs | Adjusts issue metadata for the HostApp test page. |
| namespace Maui.Controls.Sample.Issues; | ||
|
|
||
| [Issue(IssueTracker.Github, 34898, "Shell.Items.Clear does not disconnect handlers correctly", PlatformAffected.iOS | PlatformAffected.Android | PlatformAffected.macOS)] | ||
| [Issue(IssueTracker.Github, 34898, "Shell.Items.Clear does not disconnect handlers correctly", PlatformAffected.All)] |
This comment has been minimized.
This comment has been minimized.
AI Review Summary
🗂️ Review Sessions — click to expand🚦 Gate — Test Before & After FixGate Result: ✅ PASSEDPlatform: WINDOWS · Base: main · Merge base:
🔴 Without fix — 🖥️ Issue34898: FAIL ✅ · 587sError-relevant lines (filtered from the build log): 🟢 With fix — 🖥️ Issue34898: PASS ✅ · 501s(no coded error found; showing last 1200 chars) 📁 Fix files reverted (1 files)
📱 UI Tests — ShellDetected UI test categories: ✅ Deep UI tests — 276 passed, 0 failed across 1 category on platform-pool agent (replaces in-process counts above). 🧪 UI Test Execution Results (deep, platform pool)
📋 Pre-Flight — Context & ValidationIssue: #35035 - [Windows]Shell.Items.Clear() does not disconnect handlers correctly Key Findings
Code Review SummaryVerdict: NEEDS_DISCUSSION Key code review findings:
Fix Candidates
🔬 Code Review — Deep AnalysisCode Review — PR #36872Independent AssessmentWhat this changes: Windows Reconciliation with PR NarrativeAuthor claims: Fixes Windows memory leak for Prior Review ReconciliationNo prior ❌ Error findings found. A prior non-error Copilot comment noted that changing External Output Contract
Blast Radius Assessment
CI Status
FindingsNo ❌ Error, Failure-Mode Probing
Verdict: NEEDS_DISCUSSIONConfidence: low 🛠️ Fix — Analysis & ComparisonFix Candidates
Cross-Pollination
Exhausted: Yes — one meaningfully different alternative passed, one failed empirically, two intended alternatives were blocked by mandatory baseline isolation, and cross-pollination only produced lifecycle variants that could not be safely applied/tested in the current dirty workspace. Attempt Narrativestry-fix-1Created a Shell-aware try-fix-2Tried Shell-level removed-item cleanup in try-fix-3Planned a content-owned cleanup from old try-fix-4Planned a narrow 🏁 Report — Final RecommendationComparative Candidate Report — PR #36872Candidate ranking
AnalysisThe PR fix and
The expert review found no actionable changes for the PR, so Winner
🧭 Next Steps — review latest findingsNo alternative fix was selected for this run. Review the session findings and CI results before merging. |
…handlers on removal (#36872) <!-- 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 On Windows, calling Shell.Items.Clear() and navigating to a new root page leaves the old page's handlers (and its children's handlers) connected, causing a memory leak ### Root Cause ShellSectionHandler.SetVirtualView() reuses the handler for the new ShellSection but never disconnects handlers from the old section's pages when the section is removed from the Shell hierarchy. ### Description of Change Before the StackNavigationManager switches to the new section, check if the old ShellSection is no longer part of the Shell (FindParentOfType<Shell>() is null). If so, iterate NavigationStack and call DisconnectHandlers() on each page to recursively free all native resources. Validated the behavior in the following platforms - [ ] Android - [x] Windows - [ ] iOS - [ ] Mac ### Issues Fixed Fixes #35035 ### Test case The [Issue34898](https://github.com/dotnet/maui/blob/main/src/Controls/tests/TestCases.HostApp/Issues/Issue34898.cs#L1) test case is already present in the main source code. ### Output ScreenShot |Before|After| |--|--| | <video src="https://github.com/user-attachments/assets/87e4c6e5-ccbf-45ee-9b78-a19c50a904d0" >| <video src="https://github.com/user-attachments/assets/03b3b338-765a-4b54-9103-eb880aa580a7">|
…handlers on removal (#36872) <!-- 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 On Windows, calling Shell.Items.Clear() and navigating to a new root page leaves the old page's handlers (and its children's handlers) connected, causing a memory leak ### Root Cause ShellSectionHandler.SetVirtualView() reuses the handler for the new ShellSection but never disconnects handlers from the old section's pages when the section is removed from the Shell hierarchy. ### Description of Change Before the StackNavigationManager switches to the new section, check if the old ShellSection is no longer part of the Shell (FindParentOfType<Shell>() is null). If so, iterate NavigationStack and call DisconnectHandlers() on each page to recursively free all native resources. Validated the behavior in the following platforms - [ ] Android - [x] Windows - [ ] iOS - [ ] Mac ### Issues Fixed Fixes #35035 ### Test case The [Issue34898](https://github.com/dotnet/maui/blob/main/src/Controls/tests/TestCases.HostApp/Issues/Issue34898.cs#L1) test case is already present in the main source code. ### Output ScreenShot |Before|After| |--|--| | <video src="https://github.com/user-attachments/assets/87e4c6e5-ccbf-45ee-9b78-a19c50a904d0" >| <video src="https://github.com/user-attachments/assets/03b3b338-765a-4b54-9103-eb880aa580a7">|
…handlers on removal (#36872) <!-- 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 On Windows, calling Shell.Items.Clear() and navigating to a new root page leaves the old page's handlers (and its children's handlers) connected, causing a memory leak ### Root Cause ShellSectionHandler.SetVirtualView() reuses the handler for the new ShellSection but never disconnects handlers from the old section's pages when the section is removed from the Shell hierarchy. ### Description of Change Before the StackNavigationManager switches to the new section, check if the old ShellSection is no longer part of the Shell (FindParentOfType<Shell>() is null). If so, iterate NavigationStack and call DisconnectHandlers() on each page to recursively free all native resources. Validated the behavior in the following platforms - [ ] Android - [x] Windows - [ ] iOS - [ ] Mac ### Issues Fixed Fixes #35035 ### Test case The [Issue34898](https://github.com/dotnet/maui/blob/main/src/Controls/tests/TestCases.HostApp/Issues/Issue34898.cs#L1) test case is already present in the main source code. ### Output ScreenShot |Before|After| |--|--| | <video src="https://github.com/user-attachments/assets/87e4c6e5-ccbf-45ee-9b78-a19c50a904d0" >| <video src="https://github.com/user-attachments/assets/03b3b338-765a-4b54-9103-eb880aa580a7">|
…handlers on removal (#36872) <!-- 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 On Windows, calling Shell.Items.Clear() and navigating to a new root page leaves the old page's handlers (and its children's handlers) connected, causing a memory leak ### Root Cause ShellSectionHandler.SetVirtualView() reuses the handler for the new ShellSection but never disconnects handlers from the old section's pages when the section is removed from the Shell hierarchy. ### Description of Change Before the StackNavigationManager switches to the new section, check if the old ShellSection is no longer part of the Shell (FindParentOfType<Shell>() is null). If so, iterate NavigationStack and call DisconnectHandlers() on each page to recursively free all native resources. Validated the behavior in the following platforms - [ ] Android - [x] Windows - [ ] iOS - [ ] Mac ### Issues Fixed Fixes #35035 ### Test case The [Issue34898](https://github.com/dotnet/maui/blob/main/src/Controls/tests/TestCases.HostApp/Issues/Issue34898.cs#L1) test case is already present in the main source code. ### Output ScreenShot |Before|After| |--|--| | <video src="https://github.com/user-attachments/assets/87e4c6e5-ccbf-45ee-9b78-a19c50a904d0" >| <video src="https://github.com/user-attachments/assets/03b3b338-765a-4b54-9103-eb880aa580a7">|
…handlers on removal (#36872) <!-- 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 On Windows, calling Shell.Items.Clear() and navigating to a new root page leaves the old page's handlers (and its children's handlers) connected, causing a memory leak ### Root Cause ShellSectionHandler.SetVirtualView() reuses the handler for the new ShellSection but never disconnects handlers from the old section's pages when the section is removed from the Shell hierarchy. ### Description of Change Before the StackNavigationManager switches to the new section, check if the old ShellSection is no longer part of the Shell (FindParentOfType<Shell>() is null). If so, iterate NavigationStack and call DisconnectHandlers() on each page to recursively free all native resources. Validated the behavior in the following platforms - [ ] Android - [x] Windows - [ ] iOS - [ ] Mac ### Issues Fixed Fixes #35035 ### Test case The [Issue34898](https://github.com/dotnet/maui/blob/main/src/Controls/tests/TestCases.HostApp/Issues/Issue34898.cs#L1) test case is already present in the main source code. ### Output ScreenShot |Before|After| |--|--| | <video src="https://github.com/user-attachments/assets/87e4c6e5-ccbf-45ee-9b78-a19c50a904d0" >| <video src="https://github.com/user-attachments/assets/03b3b338-765a-4b54-9103-eb880aa580a7">|
…handlers on removal (#36872) <!-- 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 On Windows, calling Shell.Items.Clear() and navigating to a new root page leaves the old page's handlers (and its children's handlers) connected, causing a memory leak ### Root Cause ShellSectionHandler.SetVirtualView() reuses the handler for the new ShellSection but never disconnects handlers from the old section's pages when the section is removed from the Shell hierarchy. ### Description of Change Before the StackNavigationManager switches to the new section, check if the old ShellSection is no longer part of the Shell (FindParentOfType<Shell>() is null). If so, iterate NavigationStack and call DisconnectHandlers() on each page to recursively free all native resources. Validated the behavior in the following platforms - [ ] Android - [x] Windows - [ ] iOS - [ ] Mac ### Issues Fixed Fixes #35035 ### Test case The [Issue34898](https://github.com/dotnet/maui/blob/main/src/Controls/tests/TestCases.HostApp/Issues/Issue34898.cs#L1) test case is already present in the main source code. ### Output ScreenShot |Before|After| |--|--| | <video src="https://github.com/user-attachments/assets/87e4c6e5-ccbf-45ee-9b78-a19c50a904d0" >| <video src="https://github.com/user-attachments/assets/03b3b338-765a-4b54-9103-eb880aa580a7">|
…handlers on removal (#36872) <!-- 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 On Windows, calling Shell.Items.Clear() and navigating to a new root page leaves the old page's handlers (and its children's handlers) connected, causing a memory leak ### Root Cause ShellSectionHandler.SetVirtualView() reuses the handler for the new ShellSection but never disconnects handlers from the old section's pages when the section is removed from the Shell hierarchy. ### Description of Change Before the StackNavigationManager switches to the new section, check if the old ShellSection is no longer part of the Shell (FindParentOfType<Shell>() is null). If so, iterate NavigationStack and call DisconnectHandlers() on each page to recursively free all native resources. Validated the behavior in the following platforms - [ ] Android - [x] Windows - [ ] iOS - [ ] Mac ### Issues Fixed Fixes #35035 ### Test case The [Issue34898](https://github.com/dotnet/maui/blob/main/src/Controls/tests/TestCases.HostApp/Issues/Issue34898.cs#L1) test case is already present in the main source code. ### Output ScreenShot |Before|After| |--|--| | <video src="https://github.com/user-attachments/assets/87e4c6e5-ccbf-45ee-9b78-a19c50a904d0" >| <video src="https://github.com/user-attachments/assets/03b3b338-765a-4b54-9103-eb880aa580a7">|
…handlers on removal (#36872) <!-- 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 On Windows, calling Shell.Items.Clear() and navigating to a new root page leaves the old page's handlers (and its children's handlers) connected, causing a memory leak ### Root Cause ShellSectionHandler.SetVirtualView() reuses the handler for the new ShellSection but never disconnects handlers from the old section's pages when the section is removed from the Shell hierarchy. ### Description of Change Before the StackNavigationManager switches to the new section, check if the old ShellSection is no longer part of the Shell (FindParentOfType<Shell>() is null). If so, iterate NavigationStack and call DisconnectHandlers() on each page to recursively free all native resources. Validated the behavior in the following platforms - [ ] Android - [x] Windows - [ ] iOS - [ ] Mac ### Issues Fixed Fixes #35035 ### Test case The [Issue34898](https://github.com/dotnet/maui/blob/main/src/Controls/tests/TestCases.HostApp/Issues/Issue34898.cs#L1) test case is already present in the main source code. ### Output ScreenShot |Before|After| |--|--| | <video src="https://github.com/user-attachments/assets/87e4c6e5-ccbf-45ee-9b78-a19c50a904d0" >| <video src="https://github.com/user-attachments/assets/03b3b338-765a-4b54-9103-eb880aa580a7">|
…handlers on removal (#36872) <!-- 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 On Windows, calling Shell.Items.Clear() and navigating to a new root page leaves the old page's handlers (and its children's handlers) connected, causing a memory leak ### Root Cause ShellSectionHandler.SetVirtualView() reuses the handler for the new ShellSection but never disconnects handlers from the old section's pages when the section is removed from the Shell hierarchy. ### Description of Change Before the StackNavigationManager switches to the new section, check if the old ShellSection is no longer part of the Shell (FindParentOfType<Shell>() is null). If so, iterate NavigationStack and call DisconnectHandlers() on each page to recursively free all native resources. Validated the behavior in the following platforms - [ ] Android - [x] Windows - [ ] iOS - [ ] Mac ### Issues Fixed Fixes #35035 ### Test case The [Issue34898](https://github.com/dotnet/maui/blob/main/src/Controls/tests/TestCases.HostApp/Issues/Issue34898.cs#L1) test case is already present in the main source code. ### Output ScreenShot |Before|After| |--|--| | <video src="https://github.com/user-attachments/assets/87e4c6e5-ccbf-45ee-9b78-a19c50a904d0" >| <video src="https://github.com/user-attachments/assets/03b3b338-765a-4b54-9103-eb880aa580a7">|
…handlers on removal (#36872) <!-- 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 On Windows, calling Shell.Items.Clear() and navigating to a new root page leaves the old page's handlers (and its children's handlers) connected, causing a memory leak ### Root Cause ShellSectionHandler.SetVirtualView() reuses the handler for the new ShellSection but never disconnects handlers from the old section's pages when the section is removed from the Shell hierarchy. ### Description of Change Before the StackNavigationManager switches to the new section, check if the old ShellSection is no longer part of the Shell (FindParentOfType<Shell>() is null). If so, iterate NavigationStack and call DisconnectHandlers() on each page to recursively free all native resources. Validated the behavior in the following platforms - [ ] Android - [x] Windows - [ ] iOS - [ ] Mac ### Issues Fixed Fixes #35035 ### Test case The [Issue34898](https://github.com/dotnet/maui/blob/main/src/Controls/tests/TestCases.HostApp/Issues/Issue34898.cs#L1) test case is already present in the main source code. ### Output ScreenShot |Before|After| |--|--| | <video src="https://github.com/user-attachments/assets/87e4c6e5-ccbf-45ee-9b78-a19c50a904d0" >| <video src="https://github.com/user-attachments/assets/03b3b338-765a-4b54-9103-eb880aa580a7">|
…handlers on removal (#36872) <!-- 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 On Windows, calling Shell.Items.Clear() and navigating to a new root page leaves the old page's handlers (and its children's handlers) connected, causing a memory leak ### Root Cause ShellSectionHandler.SetVirtualView() reuses the handler for the new ShellSection but never disconnects handlers from the old section's pages when the section is removed from the Shell hierarchy. ### Description of Change Before the StackNavigationManager switches to the new section, check if the old ShellSection is no longer part of the Shell (FindParentOfType<Shell>() is null). If so, iterate NavigationStack and call DisconnectHandlers() on each page to recursively free all native resources. Validated the behavior in the following platforms - [ ] Android - [x] Windows - [ ] iOS - [ ] Mac ### Issues Fixed Fixes #35035 ### Test case The [Issue34898](https://github.com/dotnet/maui/blob/main/src/Controls/tests/TestCases.HostApp/Issues/Issue34898.cs#L1) test case is already present in the main source code. ### Output ScreenShot |Before|After| |--|--| | <video src="https://github.com/user-attachments/assets/87e4c6e5-ccbf-45ee-9b78-a19c50a904d0" >| <video src="https://github.com/user-attachments/assets/03b3b338-765a-4b54-9103-eb880aa580a7">|
…handlers on removal (#36872) <!-- 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 On Windows, calling Shell.Items.Clear() and navigating to a new root page leaves the old page's handlers (and its children's handlers) connected, causing a memory leak ### Root Cause ShellSectionHandler.SetVirtualView() reuses the handler for the new ShellSection but never disconnects handlers from the old section's pages when the section is removed from the Shell hierarchy. ### Description of Change Before the StackNavigationManager switches to the new section, check if the old ShellSection is no longer part of the Shell (FindParentOfType<Shell>() is null). If so, iterate NavigationStack and call DisconnectHandlers() on each page to recursively free all native resources. Validated the behavior in the following platforms - [ ] Android - [x] Windows - [ ] iOS - [ ] Mac ### Issues Fixed Fixes #35035 ### Test case The [Issue34898](https://github.com/dotnet/maui/blob/main/src/Controls/tests/TestCases.HostApp/Issues/Issue34898.cs#L1) test case is already present in the main source code. ### Output ScreenShot |Before|After| |--|--| | <video src="https://github.com/user-attachments/assets/87e4c6e5-ccbf-45ee-9b78-a19c50a904d0" >| <video src="https://github.com/user-attachments/assets/03b3b338-765a-4b54-9103-eb880aa580a7">|
…handlers on removal (#36872) <!-- 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 On Windows, calling Shell.Items.Clear() and navigating to a new root page leaves the old page's handlers (and its children's handlers) connected, causing a memory leak ### Root Cause ShellSectionHandler.SetVirtualView() reuses the handler for the new ShellSection but never disconnects handlers from the old section's pages when the section is removed from the Shell hierarchy. ### Description of Change Before the StackNavigationManager switches to the new section, check if the old ShellSection is no longer part of the Shell (FindParentOfType<Shell>() is null). If so, iterate NavigationStack and call DisconnectHandlers() on each page to recursively free all native resources. Validated the behavior in the following platforms - [ ] Android - [x] Windows - [ ] iOS - [ ] Mac ### Issues Fixed Fixes #35035 ### Test case The [Issue34898](https://github.com/dotnet/maui/blob/main/src/Controls/tests/TestCases.HostApp/Issues/Issue34898.cs#L1) test case is already present in the main source code. ### Output ScreenShot |Before|After| |--|--| | <video src="https://github.com/user-attachments/assets/87e4c6e5-ccbf-45ee-9b78-a19c50a904d0" >| <video src="https://github.com/user-attachments/assets/03b3b338-765a-4b54-9103-eb880aa580a7">|
…handlers on removal (#36872) <!-- 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 On Windows, calling Shell.Items.Clear() and navigating to a new root page leaves the old page's handlers (and its children's handlers) connected, causing a memory leak ### Root Cause ShellSectionHandler.SetVirtualView() reuses the handler for the new ShellSection but never disconnects handlers from the old section's pages when the section is removed from the Shell hierarchy. ### Description of Change Before the StackNavigationManager switches to the new section, check if the old ShellSection is no longer part of the Shell (FindParentOfType<Shell>() is null). If so, iterate NavigationStack and call DisconnectHandlers() on each page to recursively free all native resources. Validated the behavior in the following platforms - [ ] Android - [x] Windows - [ ] iOS - [ ] Mac ### Issues Fixed Fixes #35035 ### Test case The [Issue34898](https://github.com/dotnet/maui/blob/main/src/Controls/tests/TestCases.HostApp/Issues/Issue34898.cs#L1) test case is already present in the main source code. ### Output ScreenShot |Before|After| |--|--| | <video src="https://github.com/user-attachments/assets/87e4c6e5-ccbf-45ee-9b78-a19c50a904d0" >| <video src="https://github.com/user-attachments/assets/03b3b338-765a-4b54-9103-eb880aa580a7">|
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
On Windows, calling Shell.Items.Clear() and navigating to a new root page leaves the old page's handlers (and its children's handlers) connected, causing a memory leak
Root Cause
ShellSectionHandler.SetVirtualView() reuses the handler for the new ShellSection but never disconnects handlers from the old section's pages when the section is removed from the Shell hierarchy.
Description of Change
Before the StackNavigationManager switches to the new section, check if the old ShellSection is no longer part of the Shell (FindParentOfType() is null). If so, iterate NavigationStack and call DisconnectHandlers() on each page to recursively free all native resources.
Validated the behavior in the following platforms
Issues Fixed
Fixes #35035
Test case
The Issue34898 test case is already present in the main source code.
Output ScreenShot
35035-BeforeFix.mp4
35035-AfterFix.mp4