Skip to content
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

[iOS] Fixed memory leak with PopToRootAsync when using TitleView #28547

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Vignesh-SF3580
Copy link
Contributor

@Vignesh-SF3580 Vignesh-SF3580 commented Mar 21, 2025

Issue Detail

When navigating using PushAsync and PopToRootAsync, the destructor was not invoked when using PopToRootAsync.

Root Cause

The TitleView was not properly disposed when using PopToRootAsync.

Description of Change

Resolved the issue by disposing the TitleView using Disconnect, similar to PopAsync.

Reference: https://github.com/dotnet/maui/blob/main/src/Controls/src/Core/Compatibility/Handlers/NavigationPage/iOS/NavigationRenderer.cs#L345

Issues Fixed

Fixes #28201

Screenshots

Before Issue Fix After Issue Fix
28201BeforeFix.mov
28201AfterFix.mov

Copy link
Contributor

Hey there @Vignesh-SF3580! 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 dotnet-policy-service bot added community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration labels Mar 21, 2025
@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@Vignesh-SF3580 Vignesh-SF3580 marked this pull request as ready for review March 21, 2025 10:52
@Copilot Copilot bot review requested due to automatic review settings March 21, 2025 10:52
@Vignesh-SF3580 Vignesh-SF3580 requested a review from a team as a code owner March 21, 2025 10:52
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a memory leak issue by ensuring that the TitleView is correctly disposed when navigating back to the root page on iOS. Key changes include:

  • Adding disposal logic in NavigationRenderer.cs for popped view controllers.
  • Introducing new test cases in both TestCases.Shared.Tests and TestCases.HostApp to validate that the TitleView is disposed.
  • Updating navigation behavior to prevent memory leaks during PopToRootAsync.

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue28201.cs Adds a test case to check page disposal behavior
src/Controls/tests/TestCases.HostApp/Issues/Issue28201.xaml.cs Updates the UI test page for validating TitleView disposal
src/Controls/src/Core/Compatibility/Handlers/NavigationPage/iOS/NavigationRenderer.cs Enhances PopToRootAsync to dispose popped controllers properly
Files not reviewed (1)
  • src/Controls/tests/TestCases.HostApp/Issues/Issue28201.xaml: Language not supported

@bhavanesh2001
Copy link
Contributor

The title is bit confusing. Does the leak has something to do with TitleView?

@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Copy link
Contributor

@jsuarezruiz jsuarezruiz left a comment

Choose a reason for hiding this comment

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

Could you review if can include a test inside the MemoryTests.cs class to verify that popping a Page using TitleView does not leak?

Can use the PagesDoNotLeak as reference.

@@ -310,11 +310,26 @@ protected virtual async Task<bool> OnPopToRoot(Page page, bool animated)

var task = GetAppearedOrDisappearedTask(page);

PopToRootViewController(animated);
var poppedControllers = PopToRootViewController(animated);
Copy link
Contributor

Choose a reason for hiding this comment

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

A this point, this method should be responsible for managing the cleanup of the ViewController that is currently at the top of the view controller stack. It ensures proper disposal of resources if the user action leads to a view being removed from the visual stack. I think inside this method, we should ensure we dispose the TitleView if exists.

Something like:
NavigationItem.TitleView.Dispose();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jsuarezruiz In PopToRootViewController, the removal and disposal of controllers are restricted by the _ignorePopCall variable. Removing _ignorePopCall resolves the issue, as the removed controllers are disposed of in the RemoveViewControllers method. However, I am unsure if removing _ignorePopCall could break any existing behaviors, as the same approach is followed in Xamarin.
Therefore, I applied the fix by calling Disconnect, referring to the approach used in PopViewAsync. Could you please review and share your thoughts?

@Vignesh-SF3580
Copy link
Contributor Author

The title is bit confusing. Does the leak has something to do with TitleView?

Yes, the destructor is not invoked when using PopToRootAsync with TitleView. However, it is invoked properly when TitleView is not used.

@Vignesh-SF3580
Copy link
Contributor Author

Could you review if can include a test inside the MemoryTests.cs class to verify that popping a Page using TitleView does not leak?

Can use the PagesDoNotLeak as reference.

@jsuarezruiz The issue is only reproduced when using TitleView through a XAML file and does not occur when using it through C# code. Therefore, I added a UI test to verify this issue.
I couldn't find a reference for including a new XAML file in a MemoryTest. Could you please share your suggestions on how to handle this?

@jfversluis jfversluis added area-controls-titleview TitleView t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) labels Mar 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-titleview TitleView community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/iOS 🍎 t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NavigationPage.TitleView causes memory leak with PopToRootAsync
4 participants