Skip to content

Subscribe FlyoutPage to DeviceDisplay weakly - #40520

Merged
kubaflo merged 2 commits into
dotnet:inflight/11.0.1xx-sr3from
MykhailoDav:fix-flyoutpage-devicedisplay-leak
Nov 23, 2026
Merged

kubaflo merged 2 commits into
dotnet:inflight/11.0.1xx-sr3from
MykhailoDav:fix-flyoutpage-devicedisplay-leak

Conversation

@MykhailoDav

Copy link
Copy Markdown
Contributor

Description of Change

FlyoutPage has the same static DeviceDisplay.MainDisplayInfoChanged subscription that #40453 fixed for OrientationStateTrigger and DisplayRotationStateTrigger. It subscribes when its window is set and unsubscribes only when the window becomes null. That causes two problems:

  • Window.Destroying doesn't clear Page, so on Android every Activity recreation leaves the previous FlyoutPage rooted by the static event when CreateWindow returns a new one.
  • When a page moves straight from one window to another (for example a page reused through DI), it subscribes twice. The handler then runs twice per display change, and one subscription is left behind after the page leaves the window.

FlyoutPage now subscribes through WeakDisplayInfoChangedProxy, the proxy added in #40453. It keeps the delegate in a field, because the proxy holds the handler weakly. Subscribe drops the previous subscription first, which fixes the double subscription. OnMainDisplayInfoChanged is unchanged.

#40453 is in candidate/11.0.1xx-sr2 but not yet in net11. This PR adds the proxy class to WeakEventProxy.cs with exactly the content #40453 gave the file, so merging into a branch that already has #40453 doesn't change it.

Tests

Two tests in FlyoutPageUnitTests:

  • FlyoutPageInWindowDoesNotStayAlive puts a FlyoutPage in a window, drops both, and checks the page is collected. Without the fix: FlyoutPage should not be alive!
  • FlyoutPageMovedToAnotherWindowHandlesDisplayChangesOnce moves the page from one window to another, checks that one display change updates FlyoutBehavior once, and that nothing updates after the page leaves the window. Without the fix: Expected: 1, Actual: 2.

FlyoutPage tests: 55/57 → 57/57. Full Controls.Core.UnitTests: 6735 passed, 1 failed, the same failure as on clean net11 (TwoWayBindingToNullableDoublePropertyWithEmptyStringBecomesNull, a decimal-comma locale issue on my machine).

Before / after (sandbox app, Android emulator API 37, Release): CreateWindow returns a new FlyoutPage. The font scale was changed four times to recreate the Activity, and the label shows how many FlyoutPage instances are still alive after a GC.

Before: 5 of 5 FlyoutPages still alive After: 1 of 5 alive
before after

Full clips: before.mp4 · after.mp4

Not checked: iOS, Mac Catalyst and Windows (the change is in shared code), and the window-to-window case in a real app (unit test only).

Issues Fixed

Related to #40450

@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 -- 40520

Or

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

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

Copy link
Copy Markdown
Contributor

Hey there @MykhailoDav! 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

This comment has been minimized.

@kubaflo

kubaflo commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

/review expert-review-with-gate

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

Copy link
Copy Markdown
Contributor

Warning

Your /review gate couldn't start due to a transient error triggering the pipeline. The command was left unacknowledged so scheduled recovery can retry it automatically; you can also comment /review gate again in a minute.

@github-actions github-actions Bot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 23, 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 — 1 finding

Reviewed commit 872ad64.

See inline comments for details.


updates = 0;
mockDeviceDisplay.SetMainDisplayOrientation(DisplayOrientation.Portrait);
Assert.Equal(0, updates);

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.

[moderate] Regression Prevention and Test Coverage - The post-removal assertion cannot distinguish unsubscription from handler disconnection. Setting secondWindow.Page = null calls Window.OnPageChanged, which invokes DisconnectHandlers and clears flyoutPage.Handler. Consequently, OnMainDisplayInfoChanged produces zero mapper updates even if _displayInfoProxy.Unsubscribe() is removed and the detached page remains subscribed. Reattach a mapper handler after removal, or assert the mock's subscription count, so this negative regression case detects a leftover display subscription.

@kubaflo
kubaflo changed the base branch from net11 to inflight/11.0.1xx-sr3 November 23, 2026 11:12
@kubaflo
kubaflo merged commit e97237c into dotnet:inflight/11.0.1xx-sr3 Nov 23, 2026
11 of 21 checks passed
kubaflo added a commit that referenced this pull request Nov 23, 2026
### Description of Change

`FlyoutPage` has the same static `DeviceDisplay.MainDisplayInfoChanged`
subscription that #40453 fixed for `OrientationStateTrigger` and
`DisplayRotationStateTrigger`. It subscribes when its window is set and
unsubscribes only when the window becomes null. That causes two
problems:

- `Window.Destroying` doesn't clear `Page`, so on Android every Activity
recreation leaves the previous `FlyoutPage` rooted by the static event
when `CreateWindow` returns a new one.
- When a page moves straight from one window to another (for example a
page reused through DI), it subscribes twice. The handler then runs
twice per display change, and one subscription is left behind after the
page leaves the window.

`FlyoutPage` now subscribes through `WeakDisplayInfoChangedProxy`, the
proxy added in #40453. It keeps the delegate in a field, because the
proxy holds the handler weakly. `Subscribe` drops the previous
subscription first, which fixes the double subscription.
`OnMainDisplayInfoChanged` is unchanged.

#40453 is in `candidate/11.0.1xx-sr2` but not yet in `net11`. This PR
adds the proxy class to `WeakEventProxy.cs` with exactly the content
#40453 gave the file, so merging into a branch that already has #40453
doesn't change it.

### Tests

Two tests in `FlyoutPageUnitTests`:
- `FlyoutPageInWindowDoesNotStayAlive` puts a `FlyoutPage` in a window,
drops both, and checks the page is collected. Without the fix:
`FlyoutPage should not be alive!`
- `FlyoutPageMovedToAnotherWindowHandlesDisplayChangesOnce` moves the
page from one window to another, checks that one display change updates
`FlyoutBehavior` once, and that nothing updates after the page leaves
the window. Without the fix: `Expected: 1, Actual: 2`.

FlyoutPage tests: 55/57 → 57/57. Full Controls.Core.UnitTests: 6735
passed, 1 failed, the same failure as on clean `net11`
(`TwoWayBindingToNullableDoublePropertyWithEmptyStringBecomesNull`, a
decimal-comma locale issue on my machine).

**Before / after** (sandbox app, Android emulator API 37, Release):
`CreateWindow` returns a new `FlyoutPage`. The font scale was changed
four times to recreate the Activity, and the label shows how many
`FlyoutPage` instances are still alive after a GC.

| Before: 5 of 5 FlyoutPages still alive | After: 1 of 5 alive |
|---|---|
|
![before](https://raw.githubusercontent.com/MykhailoDav/maui/media-pr-2026-11-12/flyoutpage-leak-before.png)
|
![after](https://raw.githubusercontent.com/MykhailoDav/maui/media-pr-2026-11-12/flyoutpage-leak-after.png)
|

Full clips:
[before.mp4](https://raw.githubusercontent.com/MykhailoDav/maui/media-pr-2026-11-12/flyoutpage-leak-before.mp4)
·
[after.mp4](https://raw.githubusercontent.com/MykhailoDav/maui/media-pr-2026-11-12/flyoutpage-leak-after.mp4)

Not checked: iOS, Mac Catalyst and Windows (the change is in shared
code), and the window-to-window case in a real app (unit test only).

### Issues Fixed

Related to #40450

Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit to devanathan-vaithiyanathan/maui that referenced this pull request Dec 1, 2026
Conflicts: FlyoutPage keeps the weak DeviceDisplay proxy (dotnet#40520) with net11's window-and-handler-aware subscription; MemoryTests and WindowHandlerTests.Windows keep the tests added on both sides.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 872ad643 Deployed Nov 23, 2026 by MykhailoDav via conclusion #3207
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.

2 participants