Skip to content

Fix Memory Leak for Media Element in Windows#3115

Open
ne0rrmatrix wants to merge 3 commits intoCommunityToolkit:mainfrom
ne0rrmatrix:FixMediaElementDanglingEventHandler
Open

Fix Memory Leak for Media Element in Windows#3115
ne0rrmatrix wants to merge 3 commits intoCommunityToolkit:mainfrom
ne0rrmatrix:FixMediaElementDanglingEventHandler

Conversation

@ne0rrmatrix
Copy link
Member

Description of Change

There was a button with an event attached to it that was not properly disposed of when class was being disposed. I have removed the event, it was an unneeded duplicate and moved what it did into an existing event that is unsubscribed in disposal for media element. Thus I have removed an extra event and this should fix the issue as described.

Linked Issues

PR Checklist

  • Has a linked Issue, and the Issue has been approved(bug) or Championed (feature/proposal)
  • Has tests (if omitted, state reason in description)
  • Has samples (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard
  • Documentation created or updated: https://github.com/MicrosoftDocs/CommunityToolkit/pulls

Additional information

This is the direct result of failing to unsubscribe from an event in the disposal method for the class. I have moved the behavior in event to another class which has a duplicate event handler. I do not know why I was using two different events. I switched to just using one and removed the offending event. I see no reason for having two events for same event. I can't explain why I would have done that.

…r class that is released when dispose is called. I see no need for two events that handle the same event. I should never have had two to start with. This fixes that.
@ne0rrmatrix ne0rrmatrix added the 📽️ MediaElement Issue/PR that has to do with MediaElement label Feb 23, 2026
@TheCodeTraveler TheCodeTraveler requested review from Copilot and removed request for TheCodeTraveler February 23, 2026 22:45
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a memory leak in MediaElement on Windows by removing a duplicate event handler that was not being properly unsubscribed during disposal. The issue was caused by FullScreenButton_Click event handler in CustomTransportControls that maintained local state and updated the fullscreen button icon, but was never unsubscribed when the control was disposed.

Changes:

  • Removed the duplicate FullScreenButton_Click event handler and isFullScreen field from CustomTransportControls
  • Moved icon state updates directly into the existing OnFullScreenButtonClick handler in MauiMediaElement, which is properly unsubscribed during disposal
  • Simplified state management by using the actual window presenter state (appWindow.Presenter.Kind) instead of maintaining a local boolean

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.windows.cs Added icon updates to the existing OnFullScreenButtonClick handler based on actual window state
src/CommunityToolkit.Maui.MediaElement/Primitives/CustomTransportControls.windows.cs Removed duplicate event handler and state field that were causing the memory leak

@jo-ruch
Copy link

jo-ruch commented Feb 24, 2026

Thank you for the quick work on this 😄 Appreciate it and the honesty 😆

Will this fix be ported to 6.x? Our app is currently still on .NET 9

@ne0rrmatrix
Copy link
Member Author

Thank you for the quick work on this 😄 Appreciate it and the honesty 😆

Will this fix be ported to 6.x? Our app is currently still on .NET 9

We only support the current version of Dotnet which is dotnet 10. We are all volunteers and supporting multiple release branches is not supported at this time.

@ne0rrmatrix ne0rrmatrix requested a review from a team March 8, 2026 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

📽️ MediaElement Issue/PR that has to do with MediaElement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Potential memory leak in MediaElement on Windows

3 participants