Skip to content

[BUG] Media Element cannot be disconnected #2388

Description

@cagriy

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

Upon exiting a page that has a MediaElement, although calling the DisconnectHandler as recommended, MediaElement does not get removed.

Expected Behavior

MediaElement should be garbage collected.

Steps To Reproduce

  1. Open and run the solution from the reproduction repository
  2. Click the button to go to the second page where the memory element lives
  3. Click the back button to go to the main page
  4. Check gcdump vie heapview and search for MediaElement

Link to public reproduction project repository

https://github.com/cagriy/MemoryLeak9

Environment

- .NET MAUI CommunityToolkit Media Element: 5.0.0
- OS: iOS 18.1
- .NET MAUI: 9.0.21

Anything else?

No response

Activity

  1. cagriy commented on Dec 18, 2024

    @cagriy
    Author

    Hi @brminnick, I've upgraded Media Element to 5.0.0, but I still see the same problem. Is there anything else I need to be doing to make sure the media element is disconnected correctly?

    The reproduction repo now includes a sample with the upgraded media element.

    Sorry if I am missing something obvious.

  2. ne0rrmatrix commented on Dec 19, 2024

    @ne0rrmatrix
    Member

    Handler disconnect is done automatically by Maui now. If you want more info: https://learn.microsoft.com/en-us/dotnet/maui/whats-new/dotnet-9?view=net-maui-9.0. You can control it yourself if you set the handler disconnect policy.

    Example:

     <toolkit:MediaElement
         x:Name="MediaElement"
         HandlerProperties.DisconnectPolicy="Manual"
         Source="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"/>
    

    The above code will let you control it manually. It is up to you if you want it to work this way now. By default it will work automatically in the background.

  3. ne0rrmatrix commented on Jan 23, 2025

    @ne0rrmatrix
    Member

    I can confirm that this is an issue with ios. I tested it for the last few hours and have found windows and android are fine buit IOS does show an error using the maui leak tool.

  4. MarcelStommel commented on May 2, 2025

    @MarcelStommel
    Contributor

    Am currently profiling memory leaks in an iPad app and found that my Media Elements aren't GC'ed either!
    Looking through the code and my memory dump, it looks like this might me the culprit:

    NowPlayingInfo.Artwork = new(boundsSize: new(320, 240), requestHandler: _ => GetImage(mediaElement.MetadataArtworkUrl));

    Here requestHandler holds a reference to the Metadata class, by capturing this implicitly in the lambda.
    Metadata then holds the MPNowPlayingInfo under the NowPlayingInfo Property
    Which holds the MPMediaItemArtwork under the Artwork Property
    Which in turn holds the requestHandler again, causing a reference cycle

    The easy fix here would be making requestHandler a static lambda, so it doesn't capture this anymore, which I think is also the intention here, since GetImage is a static method anyways.

    Also, this is currently just my theory, still have to test with the 'fix', if it even is one.

  5. pictos commented on May 2, 2025

    @pictos
    Member

    @MarcelStommel can you share this memory dump with us?

  6. MarcelStommel commented on May 5, 2025

    @MarcelStommel
    Contributor

    @pictos I don't think I can since it's an app from our customer, but I could reproduce it in small test app I wrote that just adds/removes a MediaElement from the page.

    https://github.com/MarcelStommel/MediaLeak

    After adding and removing the MediaElement it still resides in memory under iOS, even though it's handler got correctly disconnected and the element itself disposed.

    I also wrote up a quick fix which basically involves using a proxy class instead of a lambda for the event handler. This way only the proxy class itself is captured.

    public void SetMetadata(AVPlayerItem? playerItem, IMediaElement? mediaElement)
    {
    	...
    
    	var proxy = new RequestHandlerProxy(mediaElement.MetadataArtworkUrl);
    
    	...
    	NowPlayingInfo.Artwork = new(boundsSize: new(320, 240), requestHandler: proxy.RequestHandler);
    	...
    }
    
    class RequestHandlerProxy(string metadataArtworkUrl)
    {
    	public UIImage RequestHandler(CGSize size)
    	{
    		try
    		{
    			if (metadataArtworkUrl.StartsWith(Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase))
    			{
    				return UIImage.LoadFromData(NSData.FromUrl(new NSUrl(metadataArtworkUrl))) ?? defaultUIImage;
    			}
    			return defaultUIImage;
    		}
    		catch
    		{
    			return defaultUIImage;
    		}
    	}
    }
  7. pictos commented on May 7, 2025

    @pictos
    Member

    I can confirm, there's a memory leak when mediaElement is used like that. It doesn't happen if the page is removed from the navigation stack, in that scenario everything goes away

  8. locked and limited conversation to collaborators on Jan 22, 2026
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

    bugSomething isn't working📽️ MediaElementIssue/PR that has to do with MediaElement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions