I get the warning: observer object was not disposed manually with Dispose() multiple times when I start debugging my iOS project on my iPad Air

Kim Strasser 1,751 Reputation points
2025-12-15T21:53:42.7133333+00:00

I get many messages(in debug mode) in the Visual Studio Output window that I don´t understand and I don´t know where these messages come from when I debug my iOS project on my iPad Air. My iPad Air is connected to my MacBook Air.

I get the warning: observer object was not disposed manually with Dispose(). But I don´t know where this warning comes from because I only find this code in my project when I search for "observer" in my code. And here I already use .Dispose():

observer = NSNotificationCenter.DefaultCenter.AddObserver(new NSString("AVPlayerItemDidPlayToEndTimeNotification"), async (notification) =>
   {
       PlayerViewController.DismissViewController(true, null); // add observer to catch the ending event
       // Dispose AVPlayer and AVPlayerViewController
       PlayerViewController.Player.Dispose();
       PlayerViewController.Dispose();

       // Remove observer
       NSNotificationCenter.DefaultCenter.RemoveObserver(observer);

Why am I getting the warning observer object was not disposed manually with Dispose()? How can I find out which code triggered the warnings?

I have uploaded my iOS project in Release mode to TestFlight and until now it didn´t crashed. Is it possible that my iOS project won´t crash even if I get these warnings in debug mode?

INFO: Connecting to HotReload local tunnel stream from remote client... INFO: Connecting to HotReload local tunnel stream from device... INFO: [iOS HotReload] Connected to "iPad von Kim" over USB on port 11000. INFO: Connecting to remote HotReload local tunnel on port 57871... INFO: error in connection_block_invoke_2: Connection invalid INFO: 2025-12-13 11:44:12.263 ProjectiOS[667:22917] Warning: observer object was not disposed manually with Dispose() INFO: 2025-12-13 11:44:12.263 ProjectiOS[667:22917] Warning: observer object was not disposed manually with Dispose() 2025-12-13 11:44:12.264 ProjectiOS[667:22917] Warning: observer object was not disposed manually with Dispose() INFO: 2025-12-13 11:44:12.262 ProjectiOS[667:22917] Warning: observer object was not disposed manually with Dispose() 2025-12-13 11:44:12.262 ProjectiOS[667:22917] Warning: observer object was not disposed manually with Dispose() INFO: 2025-12-13 11:44:12.263 ProjectiOS[667:22917] Warning: observer object was not disposed manually with Dispose() 2025-12-13 11:44:12.263 ProjectiOS[667:22917] Warning: observer object was not disposed manually with Dispose() 2025-12-13 11:44:12.263 ProjectiOS[667:22917] Warning: observer object was not disposed manually with Dispose() 2025-12-13 11:44:12.263 ProjectiOS[667:22917] Warning: observer object was not disposed manually with Dispose() 2025-12-13 11:44:12.263 ProjectiOS[667:22917] Warning: observer object was not disposed manually with Dispose() 'Project' (Mono: Default Domain): Loaded 'C:\ProjectTestVSInsiders\Project\ProjectiOS\bin\Debug\net10.0-ios\ios-arm64\Microsoft.Maui.Controls.HotReload.Forms.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'Project' (Mono: Default Domain): Loaded 'C:\ProjectTestVSInsiders\Project\ProjectiOS\bin\Debug\net10.0-ios\ios-arm64\Microsoft.VisualStudio.DesignTools.XamlTapContract.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. Exception thrown: 'System.NullReferenceException' in ProjectiOS.dll Object reference not set to an instance of an object. An error occurred in the vsdbg debug session of project 'C:\ProjectTestVSInsiders\Project\ProjectiOS\ProjectiOS.csproj': Exception thrown: 'System.NullReferenceException' in ProjectiOS.dll Object reference not set to an instance of an object. The thread '.NET TP Worker' (24) has exited with code 0 (0x0). INFO: 2025-12-13 11:44:33.283 ProjectiOS[667:22844] Unbalanced calls to begin/end appearance transitions for <Microsoft_Xna_Framework_iOSGameViewController: 0x105d45ea0>. INFO: 2025-12-13 11:44:33.957 ProjectiOS[667:22844] -[AVAsset loadValuesAsynchronouslyForKeys:completionHandler:] invoked with unrecognized keys ( albumName, author, title, artist ). INFO: 2025-12-13 11:44:33.974 ProjectiOS[667:23236] -[AVAsset loadValuesAsynchronouslyForKeys:completionHandler:] invoked with unrecognized keys ( artwork ). INFO: 2025-12-13 11:44:34.984 ProjectiOS[667:23236] Value cannot be null. (Parameter 'data') The program 'Project' has exited with code 0 (0x0). The vsdbg debug session for project 'C:\ProjectTestVSInsiders\Project\ProjectiOS\ProjectiOS.csproj' has been stopped Failed to Stop app: An error occurred on client IDB180345 while executing a reply for topic xvs/idb/stop-app INFO: Disposing input and output streams... INFO: Disposing input and output streams... INFO: Disposing input and output streams... INFO: Disposing input and output streams... INFO: The debug console output is no longer available for reading INFO: The debug streams are no longer available for reading or writing INFO: Wrote 'exit process' to the debug stream INFO: Disposing console and debugger streams... INFO: Closing HotReload local tunnel connection from device INFO: Closing debug connection from device (USB) INFO: Closing debug connection from remote debugger (TCP) INFO: The debug streams are no longer available for reading or writing The app has been terminated.

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Le (WICLOUD CORPORATION) 6,670 Reputation points Microsoft External Staff Moderator
    2025-12-16T04:17:06.4+00:00

    Hello @Kim Strasser ,

    Try searching for these patterns across your entire solution:

    • AddObserver
    • NSNotificationCenter
    • AVPlayerViewController (video playback often creates internal observers)
    • Any custom renderers or handlers
    • Third-party libraries that might use notification observers

    You might also have observers created by MAUI/Xamarin.Forms controls themselves, especially if you’re working with media playback components.

    Issue with your current code

    There’s a problem with the disposal order:

    observer = NSNotificationCenter.DefaultCenter.AddObserver(
        new NSString("AVPlayerItemDidPlayToEndTimeNotification"), async (notification) =>
    {
        PlayerViewController.DismissViewController(true, null);
        PlayerViewController.Player.Dispose();
        PlayerViewController.Dispose();
        NSNotificationCenter.DefaultCenter.RemoveObserver(observer);
    });
    
    

    You’re removing the observer with RemoveObserver, but you’re not calling observer.Dispose() on the observer object itself. Removing it from the notification center isn’t the same as disposing the NSObject wrapper.

    Proper disposal pattern

    You should implement this at the class level where your observer lives:

    private NSObject observer;
    
    // When setting up your observer
    observer = NSNotificationCenter.DefaultCenter.AddObserver(
        AVPlayerItem.DidPlayToEndTimeNotification, 
        HandleVideoEnd);
    
    // In your cleanup code (like Dispose or view disappearing)
    public void Dispose()
    {
        if (observer != null)
        {
            NSNotificationCenter.DefaultCenter.RemoveObserver(observer);
            observer.Dispose(); // This is the critical part you’re missing
            observer = null;
        }
    }
    
    

    Will it crash in production?

    Your app might not crash immediately, but you’re leaking memory. Each undisposed observer keeps objects in memory. Over time, especially if users navigate to views that create these observers repeatedly, memory usage will grow and could eventually cause crashes or performance issues.

    Debugging steps

    1. Use Visual Studio’s memory profiler to track object allocations.
    2. Check if AVPlayerViewController or AVPlayer creates internal observers that need explicit disposal.
    3. Look for any custom renderers or platform-specific code that might create observers.
    4. Review any media playback libraries you’re using.

    Hope this helps, and happy holidays.

    1 person found this answer helpful.

  2. Q&A Assist
    2025-12-15T21:55:01.6066667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    The warning you are encountering, "observer object was not disposed manually with Dispose()," indicates that there are instances of observers that are not being properly disposed of in your code. This can occur if you add observers to the NSNotificationCenter but do not remove them appropriately, leading to potential memory leaks.

    In your provided code snippet, you are correctly removing the observer after the notification is handled:

    NSNotificationCenter.DefaultCenter.RemoveObserver(observer);
    

    However, the warning suggests that there might be other observers in your project that are not being disposed of correctly. Here are a few steps you can take to identify and resolve the issue:

    1. Search for All Observer Instances: Search your entire project for instances where NSNotificationCenter.DefaultCenter.AddObserver is called. Ensure that each observer is paired with a corresponding RemoveObserver call.
    2. Check for Conditional Logic: Make sure that the removal of observers is not conditional or dependent on certain logic that may not always execute.
    3. Use Weak References: Consider using weak references for observers to avoid retaining them unnecessarily. This can help prevent memory leaks without needing to manually dispose of them.
    4. Debugging: You can add logging around your observer additions and removals to track where they are being added and whether they are being removed appropriately. This can help you pinpoint where the issue might be occurring.
    5. Review Other Parts of Your Code: The warning may not originate from the code snippet you provided. Review other parts of your codebase where observers might be added.

    Regarding your question about the stability of your app despite these warnings: while your app may not crash in Release mode on TestFlight, it is still advisable to address these warnings. They can lead to memory leaks, which can degrade performance over time or cause crashes under certain conditions. Regular profiling and testing can help catch these issues before they become problematic.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.