Maui: [BUG] - MediaElement DisconnectHandler Crash Android App

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

Calling the DisconnectHandler of the mediaElement:

protected override async void OnDisappearing() { base.OnDisappearing();

// Stop and cleanup MediaElement when we navigate away
if (uxMediaElement.IsVisible)
{
    await Task.Delay(500); //This is to avoid crash Disposed object 
    uxMediaElement.Handler?.DisconnectHandler();
}

}

With the Version 4.0.1 this code was working well, i had to add the Delay because the app crashes.

But with the last version 4.1.0 my app always crashing when i call the DisconnectHandler method, i have tried to remove the Delay and use the Unloaded event of the Content Page but the error still ocurrs.

Expected Behavior

The DisconnectHandler working well without app crash

Steps To Reproduce

1 - Add the Toolkit:MediaElement to a Page 2 - Configure any video file to reproduce 3 - Navigate to other page 4 - Call the DisconnectHandler in the page (Step 2) 5 - Then the app crash

Link to public reproduction project repository

https://github.com/jhovelnu/MauiApp1/tree/MediaElement_Test

Environment

- .NET MAUI CommunityToolkit:4.1.0
- OS: Android
- .NET MAUI: 8.0.82

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 35 (9 by maintainers)

Most upvoted comments

The .NET MAUI team re-engineered Handler disconnection in .NET 9: https://learn.microsoft.com/en-us/dotnet/maui/whats-new/dotnet-9?view=net-maui-9.0#handler-disconnection

If you are manually disconnecting the Handler, you need to first set HandlerProperties.SetDisconnectPolicy(video, HandlerDisconnectPolicy.Manual);.

Hi, I added the HandlerProperties.SetDisconnectPolicy in the OnAppearing and now it works well, the app does not crash anymore.

Image

Image

@ne0rrmatrix I did some more digging, and the error happens in the RemoveView method in Microsoft.Maui core. image

image

The “bug” would be fixed if there was a try catch like ive done. But no idea if that would be fine. So it seems like its trying to remove the already disposed MediaElement.