Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Implemented MediaElement ObjectDisposedException fix #1870

Merged
merged 1 commit into from Jun 29, 2022
Merged

Implemented MediaElement ObjectDisposedException fix #1870

merged 1 commit into from Jun 29, 2022

Conversation

ghost
Copy link

@ghost ghost commented Jun 3, 2022

Description of Bug

FormsVideoView instance was never set to null after disposing, which would sometimes lead to accessing the disposed object and throwing ObjectDisposedException.
I have also rearranged method calls in SetVideoURI to avoid calling the base method after the await, during which the instance could get disposed because the overridden method is async void. The only downside of this approach I could think of is that the metadata (video size and duration) may update after the video has already started. Maybe we could revert this and override Dispose and set a private field isDisposed = true and then after the await check if isDisposed == false to call the base method?

Issues Fixed

Behavioral Changes

PR Checklist

  • Has a linked Issue, and the Issue has been approved
  • 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
  • Updated documentation

@ghost
Copy link

ghost commented Jun 3, 2022

CLA assistant check
All CLA requirements met.

@net-foundation-cla

This comment was marked as outdated.

@ghost ghost changed the title Implemented fix Implemented Media Element ObjectDisposedException fix Jun 3, 2022
@ghost ghost changed the title Implemented Media Element ObjectDisposedException fix Implemented MediaElement ObjectDisposedException fix Jun 3, 2022
@@ -52,10 +52,14 @@ protected void ExtractMetadata(MediaMetadataRetriever retriever)

public override async void SetVideoURI(global::Android.Net.Uri? uri, IDictionary<string, string>? headers)
{
base.SetVideoURI(uri, headers);
Copy link
Contributor

Choose a reason for hiding this comment

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

if we cache the url in a local variable, does it work?

var localUri = uri;
// rest of the code use `localUri`

Copy link
Author

Choose a reason for hiding this comment

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

It should work without any issues, but can you elaborate why we should do that? The call to the base method throws an exception (if the object is disposed), so it wouldn't matter if we cache the argument.

Copy link
Contributor

Choose a reason for hiding this comment

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

That will avoid the usage of a disposed member. I looked into the SetMetadata method and can't see any dispose logical there... So it's something else that's disposing the URL, probably because we're using an async void and it's not waited.

Copy link
Author

Choose a reason for hiding this comment

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

Take a look here. The uri that is passed down to SetVideoURI() is never disposed. To elaborate, MediaElement is working correctly unless you close the parent page while it's loading metadata, which will Dispose() the renderer and in turn dispose FormsVideoView. In the old implementation, metadata awaiting was before the base method call. If the user closes the page while we are awaiting metadata updates, then the next call to base.SetVideoURI will throw and since we are in a async void method we can't catch any exceptions outside. Of course, we can do this after awaiting:

Suggested change
base.SetVideoURI(uri, headers);
try
{
base.SetVideoURI(uri, headers);
}
catch(ObjectDisposedException) { }

but that is bad code in my opinion.

I caught this bug by accident, by opening and closing the page really fast. After rearranging them, there were no more exceptions. The other bug (not set to null after disposing) was more frequent since the MetadataRetrieved event was raised before the base call and the renderer tried to access the disposed instance first.

Copy link
Contributor

Choose a reason for hiding this comment

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

@anvuks I believe that your PR fixes the issue. I just want to make sure that your PR doesn't cause any side effects. I'll test it tomorrow and merge it if everything works during my test. Also, I invite @peterfoot to take a look on this if he can

Copy link

Choose a reason for hiding this comment

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

@pictos @anvuks can you guys tell me where can i find the file that need to be edited by adding try and catch ???

@pictos
Copy link
Contributor

pictos commented Jun 4, 2022

@anvuks before we merge this you need to assign the CLA, can you do that please?

@ghost
Copy link
Author

ghost commented Jun 4, 2022

@pictos I already did when I submitted this PR, but for some reason the bot says it's not signed. Does it usually take this long or do I have to do something else? I've also clicked on recheck link but nothing happens.

@pictos
Copy link
Contributor

pictos commented Jun 4, 2022

@anvuks sometimes it gets crazy hehe
I'll ask someone on the top check if everything is good.

@ghost ghost closed this Jun 14, 2022
@ghost ghost reopened this Jun 14, 2022
@ghost
Copy link
Author

ghost commented Jun 14, 2022

I have closed and reopened this PR to try to fix pending cla issue by triggering the PR webhook without any luck and just noticed that microsoft-cla bot's comment has been overwritten by the net-foundation-cla bot with the same CLA Signed message. I don't know how it missed, but there is nothing I can do about it besides opening another PR.

If it's okay with you, can you merge this PR?

@jfversluis
Copy link
Member

As far as the CLA goes: one is green so that should be all good! :)

@brettnguyen
Copy link

So when will this be approved to be merged?

@brettnguyen
Copy link

I have closed and reopened this PR to try to fix pending cla issue by triggering the PR webhook without any luck and just noticed that microsoft-cla bot's comment has been overwritten by the net-foundation-cla bot with the same CLA Signed message. I don't know how it missed, but there is nothing I can do about it besides opening another PR.

If it's okay with you, can you merge this PR?

I have closed and reopened this PR to try to fix pending cla issue by triggering the PR webhook without any luck and just noticed that microsoft-cla bot's comment has been overwritten by the net-foundation-cla bot with the same CLA Signed message. I don't know how it missed, but there is nothing I can do about it besides opening another PR.

If it's okay with you, can you merge this PR?

when will this be merged?

@ghost
Copy link
Author

ghost commented Jun 19, 2022

@brettnguyen It should be merged as soon as a maintainer approves my changes. If this fix is critical to your app, you are going to have to use a nightly release until the next stable release.

@pictos CLA issues have been resolved, so if you have the time can you merge this please? 😁

@pictos
Copy link
Contributor

pictos commented Jun 20, 2022

@anvuks I'll take a look this week asap

@brettnguyen
Copy link

@anvuks @pictos is there a workaround I can use in the meantime while I wait for this all to get checked out?

Copy link
Contributor

@pictos pictos left a comment

Choose a reason for hiding this comment

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

@anvuks thanks for this ❣️ And sorry for the long time to review and approve this.
I hope this doesn't discourage you from making more contributions.

@pictos pictos merged commit 12f9d71 into xamarin:main Jun 29, 2022
@ghost
Copy link
Author

ghost commented Jun 30, 2022

@pictos Thanks! Don't worry about it, I know the CommunityToolkit maintainers are doing it all in their free time and life sometimes gets hectic. I'll be contributing more for sure 😀

@ghost ghost deleted the media-element-dispose-fix branch June 30, 2022 00:18
@ghost ghost mentioned this pull request Sep 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
4 participants