Skip to content

NavigationPage.TitleView causes memory leak with PopToRootAsync #28201

Open
@c-ody

Description

@c-ody

Description

It seems if I add NavigationPage.TitleView into a ContentPage's XAML, the page will never be destroyed if it's popped from the navigation stack with PopToRootAsync, leading to an accumulation of pages in memory. It is destroyed if I use PopAsync instead. It is also destroyed with PopToRootAsync if I remove the NavigationPage.TitleView.

Steps to Reproduce

  1. Create a new Maui app project in Visual Studio (.NET 9)
  2. Modify App.xaml.cs, MainPage.xaml.cs, and MainPage.xaml with the below snippets
  3. Start the app in an iOS simulator (mine is iPhone 15 iOS 18.1). If you click Push, Pop, Push, Pop, etc. you will notice that after 10-20 repetitions "Destroyed" is logged to the console (as many as the number of unloaded pages), indicating that the pages were destroyed.
  4. Restart the app. If you click Push, Pop To Root, Push, Pop To Root, etc. you will not see "Destroyed" even after many repetitions. (unless you accidentally clicked Pop)
  5. (optional) Remove the NavigationPage.TitleView, and if you repeat step 4 you will see that now "Destroyed" is being logged.

MainPage.xaml (and replace MauiApp7 in x:Class with your project name)

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp7.MainPage">
    <NavigationPage.TitleView>
        <Label>Test</Label>
    </NavigationPage.TitleView>
    <ScrollView>
        <VerticalStackLayout
            Padding="30,0"
            Spacing="25">
            <Image
                Source="dotnet_bot.png"
                HeightRequest="185"
                Aspect="AspectFit"
                SemanticProperties.Description="dot net bot in a hovercraft number nine" />

            <Label
                Text="Hello, World!"
                Style="{StaticResource Headline}"
                SemanticProperties.HeadingLevel="Level1" />

            <Label
                Text="Welcome to &#10;.NET Multi-platform App UI"
                Style="{StaticResource SubHeadline}"
                SemanticProperties.HeadingLevel="Level2"
                SemanticProperties.Description="Welcome to dot net Multi platform App U I" />

            <Button
                x:Name="PushBtn"
                Text="Push" 
                Clicked="OnPushClicked"
                HorizontalOptions="Fill" />

            <Button
                x:Name="PopBtn"
                Text="Pop" 
                Clicked="OnPopClicked"
                HorizontalOptions="Fill" />

            <Button
                x:Name="PopToRootBtn"
                Text="Pop To Root" 
                Clicked="OnPopToRootClicked"
                HorizontalOptions="Fill" />
        </VerticalStackLayout>
    </ScrollView>
</ContentPage>

MainPage.xaml.cs

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        Console.WriteLine("Created");
        InitializeComponent();
    }

    ~MainPage()
    {
        Console.WriteLine("Destroyed");
    }

    private void OnPushClicked(object sender, EventArgs e)
    {
        Navigation.PushAsync(new MainPage(), true);
    }

    private void OnPopClicked(object sender, EventArgs e)
    {
        if (Navigation.NavigationStack.Count > 0)
            Navigation.PopAsync(true);
    }

    private void OnPopToRootClicked(object sender, EventArgs e)
    {
        Navigation.PopToRootAsync(true);
    }
}

App.xaml.cs

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
    }

    protected override Window CreateWindow(IActivationState? activationState)
    {
        return new Window(new NavigationPage(new MainPage()));
    }
}

Link to public reproduction project repository

No response

Version with bug

9.0.40 SR4

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS, I was not able test on other platforms

Affected platform versions

iOS 18.1

Did you find any workaround?

Use PopAsync instead

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-controls-titleviewTitleViewarea-navigationNavigationPagememory-leak 💦Memory usage grows / objects live forever (sub: perf)p/2Work that is important, but is currently not scheduled for releasepartner/syncfusionIssues / PR's with Syncfusion collaborationplatform/iOS 🍎s/triagedIssue has been reviewedt/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions