Skip to content

ObjectDisposedException when Unloaded triggers with Sentry.Maui >=5.7.0-beta.0 #4474

@blazej-kosmowski

Description

@blazej-kosmowski

Package

Sentry

.NET Flavor

.NET

.NET Version

9.0.303

OS

iOS

OS Version

iOS 17.x 18.6 18.5

Development Environment

Rider 2024 (MacOS)

SDK Version

5.7.0-beta0

Self-Hosted Sentry Version

No response

Workload Versions

maui 9.0.100

UseSentry or SentrySdk.Init call

public virtual void SetupSentry(SentryMauiOptions options)
{
    options.Dsn = "...";
    options.Debug = AppConstants.EnableDebugLog;
    options.Environment = AppConstants.Environment; 
    options.TracesSampleRate = AppConstants.EnableDebugLog
        ? TracesSampleRateDebug
        : TracesSampleRateProd;
    options.SendDefaultPii = true;
    options.SetBeforeSend(ev =>
    {
        ev.Contexts["YourApp"] = new
        {
            Version = GetAppVersion(),
            UserInfo = GetUserInfo()
        };
        // ... rest of logic
        return ev;
    });
}

Steps to Reproduce

  1. Create a .NET MAUI iOS app.
  2. Enable Sentry.
  3. Add a CollectionView or repeatedly push/pop pages (navigation).
  4. Run on iOS device or simulator.
  5. After few iterations, app crashes.

Expected Result

No crash.
Breadcrumbs and lifecycle events should be recorded without accessing disposed UIView instances.

Actual Result

We got crash

System.ObjectDisposedException: Cannot access a disposed object.
Object name: '...Renderer'
   at UIKit.UIView.get_Window()
   at Microsoft.Maui.Platform.ViewExtensions.IsLoaded(UIView uiView)
   at Microsoft.Maui.Platform.ViewExtensions.<>c__DisplayClass59_0.<OnUnloaded>g__UnLoadedCheck|4()
   at Foundation.NSAsyncActionDispatcher.Apply()
   ...

Analysis

Sentry wires up MAUI event binders in SentryMauiAppBuilderExtensions.RegisterMauiEventsBinder.
These binders (e.g. MauiEventsBinder) subscribe to element events including Unloaded.
On iOS, MAUI’s ViewExtensions.OnUnloaded calls UIView.Window. If the native UIView has already been disposed, this throws ObjectDisposedException.
With Sentry enabled, the extra subscriptions increase the chance of hitting this race. Without Sentry, the app doesn’t crash.

This matches other recent iOS crash reports:
#4241: MAUI .NET 9 ObjectDisposedException disappears without Sentry
#4295: iOS crash during CollectionView virtualization (ObjectDisposedException)

Release 5.14.1 did not contain a fix for this path. The regression started in 5.7.0-beta.0, when auto-breadcrumbs were extended and binders were changed.

Regression info

Last known good: 5.6.0
First bad: 5.7.0-beta.0
Crash reproducible on all versions since then, including 5.14.1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETPull requests that update .net codeBugSomething isn't working

    Projects

    Status

    No status

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions