Skip to content

[Windows] Shell: let flyout items release the shell on disconnect - #39591

Merged
kubaflo merged 2 commits into
dotnet:candidate/11.0.1xx-sr2from
FullDevStudio:fix/shell-flyout-items-release-shell
Nov 16, 2026
Merged

kubaflo merged 2 commits into
dotnet:candidate/11.0.1xx-sr2from
FullDevStudio:fix/shell-flyout-items-release-shell

Conversation

@FullDevStudio

Copy link
Copy Markdown
Contributor

Our app swaps between two shells, and on Windows every old shell stayed in memory with all its pages, about 90 MB per switch. A gcdump showed the path: WinUI keeps the ShellFlyoutItemView of each flyout entry alive through its CCW, and the view still holds _shell and _content (whose parent is the shell item). It only lets go of them when its DataContext changes, and that does not happen when the shell's handler disconnects.

ShellView.Disconnect now also tells every ShellFlyoutItemView under it to release its content and the shell. The cleanup that used to sit in OnDataContextChanged is moved into ReleaseContent, which both paths use, so nothing changes for a DataContext change.

I added a Windows device test that shows a shell with two flyout items, swaps the window's page, disconnects the shell handler and waits for the shell to be collected. Our workaround sets DataContext = null on each ShellFlyoutItemView after the swap; with it, twelve switches left one old shell instead of twelve.

Description of Change

Issues Fixed

Fixes #

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

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

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

Or

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

@azure-pipelines

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

@dotnet-policy-service dotnet-policy-service Bot added the community ✨ Community Contribution label Oct 23, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @@FullDevStudio! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @FullDevStudio! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expert Review — 2 findings

Reviewed commit dbd94e4.

See inline comments for details.

@kubaflo kubaflo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you check the ai's suggestions?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Clearing _shell leaves subsequent flyout measurement callbacks vulnerable to a null-reference exception.

1 open issue
What changed in this PR

This PR addresses Windows Shell retention by releasing flyout-item references when the Shell handler disconnects.

Changes:

  • Reuses content cleanup for both data-context changes and disconnects.
  • Disconnects descendant flyout-item views during Shell teardown.
  • Adds a Windows device test that checks Shell collection after a page swap.
File Description
src/​Controls/​tests/​DeviceTests/​Elements/​Shell/​ShellTests.Windows.cs Adds a garbage-collection regression test.
src/​Controls/​src/​Core/​Handlers/​Shell/​Windows/​ShellView.cs Invokes flyout-item cleanup during disconnect.
src/​Controls/​src/​Core/​Handlers/​Shell/​Windows/​ShellFlyoutItemView.cs Extracts shared cleanup and clears retained references.

Review effort: Balanced

@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Oct 23, 2026
@MauiBot

This comment has been minimized.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Oct 23, 2026
github-actions[bot]

This comment was marked as resolved.

@kubaflo
kubaflo changed the base branch from main to net11.0 October 23, 2026 22:25
@kubaflo

This comment has been minimized.

@azure-pipelines

This comment has been minimized.

@kubaflo kubaflo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there are still a few ai suggestions - could you please check?

@FullDevStudio

Copy link
Copy Markdown
Contributor Author

Hi @kubaflo, I think those are the threads from the first round. Both points are already fixed in bbb4ac4: the ShellView getter is null-safe now (_shell?.Handler?.PlatformView as ShellView), with a test that measures a flyout item after the shell disconnects, and the leak test keeps the window open and runs the GC check during the shell swap. The review on bbb4ac4 came back with 0 findings. If there's another suggestion I'm missing, point me to it and I'll fix it right away.

@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 13, 2026
@MauiBot

This comment has been minimized.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 13, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expert Review — 0 findings

Reviewed commit bbb4ac4.

@kubaflo kubaflo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you check the ai's suggestions?

@kubaflo

kubaflo commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Selected PR Tests

Manual trial: the detected selection is Windows/Shell, but the live Azure pipelines do not yet accept the new selection parameters because the pipeline changes in #40253 are not on the PR revisions/default branch. The linked UI/device builds were therefore queued with default full-suite settings; they are not Windows/Shell-filtered runs. The maui-pr request was rejected before queueing.

@FullDevStudio — targeted test selection for commit bbb4ac4.

Scope targeted tests Commit bbb4ac4

🎯 Detected coverage

  • Platforms: Windows.
  • maui-pr: full build, unit and integration validation is unchanged. dispatch failed; inspect the workflow logs.
  • UI tests: Shell on selected platforms. full-suite run.
  • Device tests: Controls.DeviceTests / Shell on selected platforms. full-suite run.

❌ Results

Tests Failed

Pipeline Run Status Passed Failed Not run
maui-pr-uitests 1683274 ⏳ running 7879 1 151
maui-pr-devicetests 1683277 ❌ failed 8978 4 288
Failed tests (5)
  • Microsoft.Maui.TestCases.Tests.Issues.Issue34848(Windows).DatePickerOpenedAndClosedEventsAreRaised — CellsCheckBoxContextActionsCustomRenderersDatePickerDispatcherDisplayAlertDisplayPromptDragAndDrop_winui_ui_tests_controls
  • Essentials.DeviceTests-packaged.WorkItemExecution — DeviceTestsWindows_windows.11.amd64.client.open_Debug
  • Does Not Leak — DeviceTestsIOS_Mono_osx.26.arm64.open_Release_Mono
  • FlyoutPage as Modal Does Not Leak — DeviceTestsIOS_Mono_osx.26.arm64.open_Release_Mono
  • Controls.DeviceTests-General.WorkItemExecution — DeviceTestsIOS_Mono_osx.26.arm64.open_Release_Mono

Comment /review tests to compare these failures with recent main runs.

Manual trial snapshot; the UI run remains in progress, so these counts are not final.


🧪 Selection notes — click to expand

Selected runs are not full-suite certification; queued does not mean passed.

Coverage notes:

  • AI output unavailable; using conservative evidence-based selection.

Manual preview; selection/reporting code under test is in #40253.

@FullDevStudio

Copy link
Copy Markdown
Contributor Author

@kubaflo I replied to the three review threads and resolved them, all fixed in bbb4ac4. The failing tests in the last run are DatePicker, Essentials and iOS leak tests, none of them touch the Windows shell code this PR changes.

@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 16, 2026
@MauiBot

MauiBot commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@FullDevStudio — existing-test verification for commit bbb4ac49.

Result SKIPPED Platform Android Commit bbb4ac49


🧪 Gate analysis — click to expand

⚠ Skipped. No runnable PR tests were selected. The fix has not been verified by this gate.

No interpretable test-group summary was produced. No additional test execution is inferred.

PR head: bbb4ac49.

Without-fix baseline: 14c3ea20.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expert Review — 0 findings

Reviewed commit bbb4ac4.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 16, 2026
@kubaflo
kubaflo changed the base branch from main to candidate/11.0.1xx-sr2 November 16, 2026 20:41
@kubaflo
kubaflo merged commit a83e97a into dotnet:candidate/11.0.1xx-sr2 Nov 16, 2026
146 of 180 checks passed
kubaflo added a commit that referenced this pull request Nov 16, 2026
…9591)

Our app swaps between two shells, and on Windows every old shell stayed
in memory with all its pages, about 90 MB per switch. A gcdump showed
the path: WinUI keeps the ShellFlyoutItemView of each flyout entry alive
through its CCW, and the view still holds _shell and _content (whose
parent is the shell item). It only lets go of them when its DataContext
changes, and that does not happen when the shell's handler disconnects.

ShellView.Disconnect now also tells every ShellFlyoutItemView under it
to release its content and the shell. The cleanup that used to sit in
OnDataContextChanged is moved into ReleaseContent, which both paths use,
so nothing changes for a DataContext change.

I added a Windows device test that shows a shell with two flyout items,
swaps the window's page, disconnects the shell handler and waits for the
shell to be collected. Our workaround sets DataContext = null on each
ShellFlyoutItemView after the swap; with it, twelve switches left one
old shell instead of twelve.

<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

### Description of Change

<!-- Enter description of the fix in this section -->

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Fixes #

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 17, 2026
…9591)

Our app swaps between two shells, and on Windows every old shell stayed
in memory with all its pages, about 90 MB per switch. A gcdump showed
the path: WinUI keeps the ShellFlyoutItemView of each flyout entry alive
through its CCW, and the view still holds _shell and _content (whose
parent is the shell item). It only lets go of them when its DataContext
changes, and that does not happen when the shell's handler disconnects.

ShellView.Disconnect now also tells every ShellFlyoutItemView under it
to release its content and the shell. The cleanup that used to sit in
OnDataContextChanged is moved into ReleaseContent, which both paths use,
so nothing changes for a DataContext change.

I added a Windows device test that shows a shell with two flyout items,
swaps the window's page, disconnects the shell handler and waits for the
shell to be collected. Our workaround sets DataContext = null on each
ShellFlyoutItemView after the swap; with it, twelve switches left one
old shell instead of twelve.

<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

### Description of Change

<!-- Enter description of the fix in this section -->

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Fixes #

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 18, 2026
…9591)

Our app swaps between two shells, and on Windows every old shell stayed
in memory with all its pages, about 90 MB per switch. A gcdump showed
the path: WinUI keeps the ShellFlyoutItemView of each flyout entry alive
through its CCW, and the view still holds _shell and _content (whose
parent is the shell item). It only lets go of them when its DataContext
changes, and that does not happen when the shell's handler disconnects.

ShellView.Disconnect now also tells every ShellFlyoutItemView under it
to release its content and the shell. The cleanup that used to sit in
OnDataContextChanged is moved into ReleaseContent, which both paths use,
so nothing changes for a DataContext change.

I added a Windows device test that shows a shell with two flyout items,
swaps the window's page, disconnects the shell handler and waits for the
shell to be collected. Our workaround sets DataContext = null on each
ShellFlyoutItemView after the swap; with it, twelve switches left one
old shell instead of twelve.

<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

### Description of Change

<!-- Enter description of the fix in this section -->

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Fixes #

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 18, 2026
…9591)

Our app swaps between two shells, and on Windows every old shell stayed
in memory with all its pages, about 90 MB per switch. A gcdump showed
the path: WinUI keeps the ShellFlyoutItemView of each flyout entry alive
through its CCW, and the view still holds _shell and _content (whose
parent is the shell item). It only lets go of them when its DataContext
changes, and that does not happen when the shell's handler disconnects.

ShellView.Disconnect now also tells every ShellFlyoutItemView under it
to release its content and the shell. The cleanup that used to sit in
OnDataContextChanged is moved into ReleaseContent, which both paths use,
so nothing changes for a DataContext change.

I added a Windows device test that shows a shell with two flyout items,
swaps the window's page, disconnects the shell handler and waits for the
shell to be collected. Our workaround sets DataContext = null on each
ShellFlyoutItemView after the swap; with it, twelve switches left one
old shell instead of twelve.

<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

### Description of Change

<!-- Enter description of the fix in this section -->

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Fixes #

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 18, 2026
…9591)

Our app swaps between two shells, and on Windows every old shell stayed
in memory with all its pages, about 90 MB per switch. A gcdump showed
the path: WinUI keeps the ShellFlyoutItemView of each flyout entry alive
through its CCW, and the view still holds _shell and _content (whose
parent is the shell item). It only lets go of them when its DataContext
changes, and that does not happen when the shell's handler disconnects.

ShellView.Disconnect now also tells every ShellFlyoutItemView under it
to release its content and the shell. The cleanup that used to sit in
OnDataContextChanged is moved into ReleaseContent, which both paths use,
so nothing changes for a DataContext change.

I added a Windows device test that shows a shell with two flyout items,
swaps the window's page, disconnects the shell handler and waits for the
shell to be collected. Our workaround sets DataContext = null on each
ShellFlyoutItemView after the swap; with it, twelve switches left one
old shell instead of twelve.

<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

### Description of Change

<!-- Enter description of the fix in this section -->

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Fixes #

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 19, 2026
…9591)

Our app swaps between two shells, and on Windows every old shell stayed
in memory with all its pages, about 90 MB per switch. A gcdump showed
the path: WinUI keeps the ShellFlyoutItemView of each flyout entry alive
through its CCW, and the view still holds _shell and _content (whose
parent is the shell item). It only lets go of them when its DataContext
changes, and that does not happen when the shell's handler disconnects.

ShellView.Disconnect now also tells every ShellFlyoutItemView under it
to release its content and the shell. The cleanup that used to sit in
OnDataContextChanged is moved into ReleaseContent, which both paths use,
so nothing changes for a DataContext change.

I added a Windows device test that shows a shell with two flyout items,
swaps the window's page, disconnects the shell handler and waits for the
shell to be collected. Our workaround sets DataContext = null on each
ShellFlyoutItemView after the swap; with it, twelve switches left one
old shell instead of twelve.

<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

### Description of Change

<!-- Enter description of the fix in this section -->

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Fixes #

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community ✨ Community Contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants