Skip to content

[BUG] Popup NullReferenceException when coming back from Browser #2595

Closed

Description

@Sebastian1989101

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

In my app I have a Help page in which I use a popup to show a "loading" state of the app while the app is uploading logs and stuff to my backend. As soon as this is done, the loading popup is hidden and also Disposed. In the next step, my app opens via Browser.LaunchAsync a contact form for the user to fill in details. On Android everything works fine. On iOS however it crashes with the following NullReferenceException when pressing the "Done" button of said Browser.

System.NullReferenceException: Object reference not set to an instance of an object.
   at CommunityToolkit.Maui.Core.Views.MauiPopup.SetShadowView(UIView& target) in /_/src/CommunityToolkit.Maui.Core/Views/Popup/MauiPopup.macios.cs:line 161
   at CommunityToolkit.Maui.Core.Views.MauiPopup.ViewDidLayoutSubviews() in /_/src/CommunityToolkit.Maui.Core/Views/Popup/MauiPopup.macios.cs:line 52
   at CommunityToolkit.Maui.Core.Views.MauiPopup.__Registrar_Callbacks__.callback_22_CommunityToolkit_Maui_Core_Views_MauiPopup_ViewDidLayoutSubviews(IntPtr pobj, IntPtr sel, IntPtr* exception_gchandle)

This is a breakdown of my app code for that:

if (UploadExtendedReport)
{
    LoadingPopup? loadingPopup = null; //this is a Toolkit Popup

    try
    {
        loadingPopup = new LoadingPopup { Message = AppResources.ProgressDialog_UploadingData };
        Shell.Current.ShowPopup(loadingPopup);

        await _httpClientService.SubmitSupportRequestDataAsync(ticketID, contactType);

        var logZipFile = ZipHelper.CreateContactZipFile(_appSettings, _logger, ticketID);
        if (!string.IsNullOrWhiteSpace(logZipFile))
            await _httpClientService.SubmitSupportRequestLogsAsync(ticketID, logZipFile);
    }
    finally
    {
        if (loadingPopup != null)
        {
            loadingPopup.Close();
            loadingPopup.DisposeIfDisposable();
        }
    }
}

await Browser.OpenAsync(reportUrl, new BrowserLaunchOptions
    {
        LaunchMode = BrowserLaunchMode.SystemPreferred,
        TitleMode = BrowserTitleMode.Show,
        PreferredToolbarColor = (Color)Application.Current!.Resources["PrimaryDarkColor"],
        PreferredControlColor = (Color)Application.Current!.Resources["DefaultTextColor"]
    });

//Crashes here when returning form the browser

Expected Behavior

App not to crash

Steps To Reproduce

See code above

Link to public reproduction project repository

n/a

Environment

- .NET MAUI CommunityToolkit: 11.1.0
- OS: iOS (crashes), Android (works)
- .NET MAUI: 9.0.40

Anything else?

No response

Activity

  1. kolodiichyk commented on Mar 29, 2025

    @kolodiichyk

    Hey @Sebastian1989101,

    Here's why this is problematic:

    Mixing async and sync operations: Since Close() is an async void method, it starts an asynchronous operation but doesn't provide a way to await its completion. The code immediately moves on to DisposeIfDisposable(), then exits the finally block and then to Browser.OpenAsync.

    The correct approach is to use loadingPopup .CloseAsync() with await

    finally
    {
            if (loadingPopup != null)
            {
                await loadingPopup.CloseAsync();
                loadingPopup.DisposeIfDisposable();
            }
    }
    
  2. locked and limited conversation to collaborators on May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions