Skip to content

[iOS]Fix: FlyoutPage memory leak #28769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 5, 2025

Conversation

bhavanesh2001
Copy link
Contributor

Description of Change

While enabling the UITest Bugzilla31255Test, which pushes a FlyoutPage modally and then pops it, I encountered a memory leak on iOS. The FlyoutPage instance was not being collected even after the modal pop.

Originally, the test case was also causing a crash because Flyout and Detail were being set to instances of Page, which is not supported. I updated them to use ContentPage, which resolved the crash.

The memory leak was caused by the renderer holding a strong reference to the virtual view (_element), which prevented the FlyoutPage from being garbage collected.

I also added a device test that reproduces the same modal flow and asserts that the page is collected by the GC. While this device test covers the same scenario as the UITest, I’ve kept both for now.

If you'd prefer to remove the UITest and rely only on the device test, let me know.

Before Fix After Fix
Screenshot 2025-04-02 at 9 17 03 PM Screenshot 2025-04-02 at 9 19 11 PM

Issues Fixed

Fixes #21206

@bhavanesh2001 bhavanesh2001 requested a review from a team as a code owner April 2, 2025 16:20
@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Apr 2, 2025
@@ -269,9 +270,7 @@ protected override void Dispose(bool disposing)
}

EmptyContainers();

Page.SendDisappearing();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this shouldn't be here. We are already calling this above

Copy link
Member

Choose a reason for hiding this comment

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

Does it matter if we just leave it?

Copy link
Contributor Author

@bhavanesh2001 bhavanesh2001 Apr 2, 2025

Choose a reason for hiding this comment

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

@PureWeen Without the PR changes, since we’re inside Dispose, the wrapper’s Element will be null at this point — so Page falls back to the strongly referenced _element:

public VisualElement Element => _viewHandlerWrapper.Element ?? _element;

With the PR changes, _element is resolved through a weak reference, so keeping the SendDisappearing() call here won’t make a difference

Let me know if you think I should keep the call.

@PureWeen PureWeen added this to the .NET 9 SR7 milestone Apr 2, 2025
@jsuarezruiz jsuarezruiz added perf/memory-leak 💦 Memory usage grows / objects live forever (sub: perf) platform/ios labels Apr 4, 2025
@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@bhavanesh2001
Copy link
Contributor Author

/rebase

@PureWeen
Copy link
Member

PureWeen commented May 2, 2025

/rebase

@bhavanesh2001 bhavanesh2001 requested a review from PureWeen May 2, 2025 15:53
@PureWeen
Copy link
Member

PureWeen commented May 2, 2025

/azp run MAUI-UITests-public

@PureWeen PureWeen moved this from Todo to Ready To Review in MAUI SDK Ongoing May 2, 2025
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

PureWeen
PureWeen previously approved these changes May 3, 2025
@github-project-automation github-project-automation bot moved this from Ready To Review to Approved in MAUI SDK Ongoing May 3, 2025
@PureWeen
Copy link
Member

PureWeen commented May 3, 2025

/azp run MAUI-DeviceTests-public

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@PureWeen PureWeen changed the base branch from main to inflight/current May 5, 2025 16:52
@PureWeen PureWeen dismissed their stale review May 5, 2025 16:52

The base branch was changed.

@PureWeen PureWeen merged commit 4c88458 into dotnet:inflight/current May 5, 2025
111 of 113 checks passed
@github-project-automation github-project-automation bot moved this from Approved to Done in MAUI SDK Ongoing May 5, 2025
PureWeen pushed a commit that referenced this pull request May 5, 2025
* fix

* enable Bugzilla31255Test

* add device test

* make _element a weak reference directly

* fix device test

* Use AssertMemoryTest in UITest
PureWeen added a commit that referenced this pull request May 8, 2025
For more information about inflight process check
https://github.com/dotnet/maui/wiki/Inflight-Branch-Process

## .NET MAUI Release Notes - inflight/candidate

This document contains release notes for changes from main branch to
inflight/candidate branch.

### MAUI Product Fixes
* [iOS] Fix: FlyoutPage memory leak by @bhavanesh001 in
#28769
* [Windows] Fix for CarouselView IsSwipeEnabled=False Prevents Visual
Navigation by @SubhikshaSF4852 in
#29286
* [Windows] Fix for Argument Exception raised when the GetStringSize
method of ICanvas called with default font by @SyedAbdulAzeemSF4852 in
#29048
* Removed frame styles by @Vignesh-SF3580 in
#29222
* [Android] Fixed the CollectionView Header and Footer Do Not Align with
Horizontal ItemsLayout When EmptyView is Displayed by @Ahamed-Ali in
#28779
* Add global xmlns in template by @StephaneDelcroix in
#29203
* Fixed - On iOS GestureRecognizers don't work on Span in a Label, which
doesn't get IsVisible (=true) update from its parent by
@KarthikRajaKalaimani in #29024
* Fixed Footer not displayed at the Bottom When EmptyView in CV2 by
@Dhivya-SF4094 in #28681
* Fixed typo in Connectivity.shared.cs by @corvinsz in
#29213

### Testing
* Re-enabled flaky UI test TextInEditorShouldScroll by
@NirmalKumarYuvaraj in #29167
* [Testing] Re-Enabled UI Test - Issue10222Test by @TamilarasanSF4853 in
#29226
* [Testing] Feature Matrix UITest Cases for CollectionView Selection
Feature by @LogishaSelvarajSF4525 in
#29165

### Dependency Updates
*No dependency updates in this release*

### Docs
*No documentation changes in this release*

### Housekeeping
*No housekeeping changes in this release*

## New Contributors
* @corvinsz made their first contribution in
#29213

**Full Changelog**:
main...inflight/candidate
SuthiYuvaraj pushed a commit to SuthiYuvaraj/maui that referenced this pull request May 9, 2025
* fix

* enable Bugzilla31255Test

* add device test

* make _element a weak reference directly

* fix device test

* Use AssertMemoryTest in UITest
@bhavanesh2001 bhavanesh2001 deleted the flyoutpage_fix branch May 13, 2025 09:33
@github-actions github-actions bot locked and limited conversation to collaborators Jun 13, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-flyoutpage FlyoutPage community ✨ Community Contribution perf/memory-leak 💦 Memory usage grows / objects live forever (sub: perf) platform/ios
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[Testing] Crash popping FlyoutPage
6 participants