Fix memory leak by changing now playing artwork request handler#3051
Merged
TheCodeTraveler merged 3 commits intoCommunityToolkit:mainfrom Jan 20, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a memory leak in the MediaElement component on iOS/macOS by refactoring how the now playing artwork request handler is managed. The memory leak occurred due to a reference cycle where MPMediaItemArtwork captured the mediaElement instance through its request handler delegate, while the Metadata class holding the MPMediaItemArtwork was itself referenced by the media element.
Changes:
- Introduced a new
RequestHandlerProxyclass that captures only the artwork URL string instead of the entire media element object - Added
CoreGraphicsusing directive for theCGSizetype - Refactored the artwork request handler to use the proxy class method reference instead of a lambda
src/CommunityToolkit.Maui.MediaElement/Primitives/Metadata.macios.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
pictos
reviewed
Jan 20, 2026
src/CommunityToolkit.Maui.MediaElement/Primitives/Metadata.macios.cs
Outdated
Show resolved
Hide resolved
ne0rrmatrix
approved these changes
Jan 20, 2026
Member
ne0rrmatrix
left a comment
There was a problem hiding this comment.
Ty for the PR. Good catch. I have tested this in the simulator and I see no issues with this.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of Change
Changes the request handler for the now playing artwork to only capture the actual url string. This breaks a reference cycle with
MPNowPlayingInfoandMPMediaItemArtworkwhereMPMediaItemArtworkkeeps itself alive through the request handler delegate capturing the whole media element which holds their handler, holding theMetadataclass, holdingMPNowPlayingInfo, which finally holdsMPMediaItemArtworkagain. I did not add a test, since the changes are minimal, just changing how the artwork url is captured to avid the reference cycle.Linked Issues
PR Checklist
approved(bug) orChampioned(feature/proposal)mainat time of PRAdditional information