You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
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:
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.
@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.
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.
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
Activity
cagriy commentedon Dec 18, 2024
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.
ne0rrmatrix commentedon Dec 19, 2024
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:
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.
ne0rrmatrix commentedon Jan 23, 2025
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.
MarcelStommel commentedon May 2, 2025
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:
Maui/src/CommunityToolkit.Maui.MediaElement/Primitives/Metadata.macios.cs
Line 86 in 2e624ca
Here
requestHandlerholds a reference to theMetadataclass, by capturingthisimplicitly in the lambda.Metadatathen holds theMPNowPlayingInfounder theNowPlayingInfoPropertyWhich holds the
MPMediaItemArtworkunder theArtworkPropertyWhich in turn holds the
requestHandleragain, causing a reference cycleThe easy fix here would be making
requestHandlera static lambda, so it doesn't capturethisanymore, which I think is also the intention here, sinceGetImageis a static method anyways.Also, this is currently just my theory, still have to test with the 'fix', if it even is one.
pictos commentedon May 2, 2025
@MarcelStommel can you share this memory dump with us?
MarcelStommel commentedon May 5, 2025
@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
MediaElementfrom the page.https://github.com/MarcelStommel/MediaLeak
After adding and removing the
MediaElementit 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.
pictos commentedon May 7, 2025
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