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

[Bug] MediaElement Disposing exception when MainPage is changed on iOS #9525

Closed
StevenGranados opened this issue Feb 11, 2020 · 21 comments · Fixed by #9531
Closed

[Bug] MediaElement Disposing exception when MainPage is changed on iOS #9525

StevenGranados opened this issue Feb 11, 2020 · 21 comments · Fixed by #9531
Labels
a/mediaelement e/2 🕑 2 i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often in-progress This issue has an associated pull request that may resolve it! p/iOS 🍎 t/bug 🐛

Comments

@StevenGranados
Copy link
Contributor

Description

=================================================================
Native Crash Reporting

Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.

=================================================================
Native stacktrace:

0x10dbcd815 - /Users/macdevdaden/Library/Developer/CoreSimulator/Devices/86CB2D85-7D2A-4118-9F55-58CD6F1C6CB3/data/Containers/Bundle/Application/D6BE2062-65DD-4159-B862-D02C3AACE324/ChatUIXForms.iOS.app/ChatUIXForms.iOS : mono_dump_native_crash_info
0x10dbc1905 - /Users/macdevdaden/Library/Developer/CoreSimulator/Devices/86CB2D85-7D2A-4118-9F55-58CD6F1C6CB3/data/Containers/Bundle/Application/D6BE2062-65DD-4159-B862-D02C3AACE324/ChatUIXForms.iOS.app/ChatUIXForms.iOS : mono_handle_native_crash
0x10dbd4aa8 - /Users/macdevdaden/Library/Developer/CoreSimulator/Devices/86CB2D85-7D2A-4118-9F55-58CD6F1C6CB3/data/Containers/Bundle/Application/D6BE2062-65DD-4159-B862-D02C3AACE324/ChatUIXForms.iOS.app/ChatUIXForms.iOS : mono_sigsegv_signal_handler_debug
0x7fff52457b5d - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libsystem_platform.dylib : _sigtramp
0x1f02d01e02bc1e02 - Unknown
0x7fff25723833 - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/Foundation.framework/Foundation : NSKeyValueWillChangeWithPerThreadPendingNotifications
0x7fff2093846d - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/AVFoundation.framework/AVFoundation : -[AVPlayer setRate:withVolumeRampDuration:playImmediately:rateChangeReason:]
0x7fff20938834 - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/AVFoundation.framework/AVFoundation : -[AVPlayer setRate:]
0x10de39319 - /Users/macdevdaden/Library/Developer/CoreSimulator/Devices/86CB2D85-7D2A-4118-9F55-58CD6F1C6CB3/data/Containers/Bundle/Application/D6BE2062-65DD-4159-B862-D02C3AACE324/ChatUIXForms.iOS.app/ChatUIXForms.iOS : xamarin_dyn_objc_msgSend
0x114eefd3a - Unknown

=================================================================
Basic Fault Address Reporting

Memory around native instruction pointer (0x7fff513fa890):0x7fff513fa880 08 5b 41 5e 41 5f 5d c3 48 85 ff 7e 09 48 8b 07 .[A^A_].H..~.H..
0x7fff513fa890 8a 40 1c 24 01 c3 31 c0 c3 48 85 ff 74 12 78 18 .@.$..1..H..t.x.
0x7fff513fa8a0 48 8b 07 48 85 c0 74 08 48 89 c7 e9 dc c7 00 00 H..H..t.H.......
0x7fff513fa8b0
48 8d 05 6c 09 02 00 c3 48 89 f8 48 c1 e8 3c 48 H..l....H..H..<H

=================================================================
Managed Stacktrace:

  at <unknown> <0xffffffff>
  at ObjCRuntime.Messaging:void_objc_msgSend <0x000e9>
  at AVFoundation.AVPlayer:Pause <0x00122>
  at Xamarin.Forms.Platform.iOS.MediaElementRenderer:Dispose <0x00253>
  at Foundation.NSObject:Dispose <0x00074>
  at Xamarin.Forms.Platform.iOS.VisualElementPackager:Dispose <0x002c5>
  at Xamarin.Forms.Platform.iOS.VisualElementPackager:Dispose <0x00071>
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1:Dispose <0x0024a>
  at Foundation.NSObject:Dispose <0x00074>
  at Xamarin.Forms.Platform.iOS.VisualElementPackager:Dispose <0x002c5>
  at Xamarin.Forms.Platform.iOS.VisualElementPackager:Dispose <0x00071>
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1:Dispose <0x0024a>
  at Foundation.NSObject:Dispose <0x00074>
  at Xamarin.Forms.Platform

.iOS.VisualElementPackager:Dispose <0x002c5>
at Xamarin.Forms.Platform.iOS.VisualElementPackager:Dispose <0x00071>
at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1:Dispose <0x0024a>
at Foundation.NSObject:Dispose <0x00074>
at Xamarin.Forms.Platform.iOS.ListViewRenderer:DisposeSubviews <0x00252>
at Xamarin.Forms.Platform.iOS.ListViewRenderer:DisposeSubviews <0x001a2>
at Xamarin.Forms.Platform.iOS.ListViewRenderer:DisposeSubviews <0x001a2>
at Xamarin.Forms.Platform.iOS.ListViewRenderer:DisposeSubviews <0x001a2>
at Xamarin.Forms.Platform.iOS.ListViewRenderer:Dispose <0x0021a>
at Foundation.NSObject:Dispose <0x00074>
at Xamarin.Forms.Platform.iOS.DisposeHelpers:DisposeModalAndChildRenderers <0x0030f>
at Xamarin.Forms.Platform.iOS.Platform:CleanUpPages <0x0019a>
at Xamarin.Forms.Platform.iOS.PlatformRenderer:Dispose <0x0018a>
at Foundation.NSObject:Dispose <0x00074>
at Xamarin.Forms.Platform.iOS.Platform:Dispose <0x000fa>
at Xamarin.Forms.Platform.iOS.Platform:Syst
em.IDisposable.Dispose <0x00071>
at Xamarin.Forms.Platform.iOS.FormsApplicationDelegate:UpdateMainPage <0x001eb>
at Xamarin.Forms.Platform.iOS.FormsApplicationDelegate:ApplicationOnPropertyChanged <0x000fa>
at Xamarin.Forms.BindableObject:OnPropertyChanged <0x00118>
at Xamarin.Forms.Element:OnPropertyChanged <0x000b2>
at Xamarin.Forms.Application:set_MainPage <0x00417>
at ChatUIXForms.Helpers.Methods:Logout <0x001ea>
at d__7:MoveNext <0x006e2>
at System.Runtime.CompilerServices.AsyncVoidMethodBuilder:Start <0x001be>
at ChatUIXForms.Views.MainPage:LogOut <0x0037a>
at Xamarin.Forms.MenuItem:OnClicked <0x000ab>
at Xamarin.Forms.MenuItem:Xamarin.Forms.IMenuItemController.Activate <0x00183>
at SecondaryToolbarItem:<.ctor>b__1_0 <0x00084>
at UIKit.UIControlEventProxy:Activated <0x000b1>
at System.Object:runtime_invoke_void__this__ <0x000e5>
at <0xffffffff>
at UIKit.UIApplication:UIApplicationMain <0x00259>
at UIKit.UIApplication:Main <0x000b2>

at UIKit.UIApplication:Main <0x00132>
at ChatUIXForms.iOS.Application:Main <0x00092>
at :runtime_invoke_void_object <0x00352>

Steps to Reproduce

Spam new page button in repro.
(In my project's case, just one page change is enough to get the exception)

Expected Behavior

New Page appears with video on it.

Actual Behavior

Video isn't disposed properly.

Basic Information

  • Version with issue: 4.5.0-pre4
  • Last known good version :N/A
  • IDE:Visual Studio 16.4.4
  • Platform Target Frameworks:
    • iOS: 13.3
    • Android: N/A
    • UWP: N/A
  • Affected Devices: iPhone 8 simulator

Reproduction Link

VideoPlayer.zip

@StevenGranados StevenGranados added s/unverified New report that has yet to be verified t/bug 🐛 labels Feb 11, 2020
@StevenGranados StevenGranados changed the title [Bug] MediaElement Disposing exception when MainPage is changed. [Bug] MediaElement Disposing exception when MainPage is changed on iOS Feb 11, 2020
@samhouts samhouts added a/mediaelement i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often p/iOS 🍎 in-progress This issue has an associated pull request that may resolve it! labels Feb 12, 2020
@samhouts samhouts added e/2 🕑 2 and removed s/unverified New report that has yet to be verified labels Feb 13, 2020
@cas8180
Copy link

cas8180 commented Apr 3, 2020

This is a hypercritical bug for me. This pretty much makes it impossible to use this control/element within any iOS app. As soon as you navigate out of the page be pressing the back button the whole app crashes. Is there a work around for this? Or way to add this to a hotfix release? I think adding this to 4.6 is a really long timeline for something that is hypercritical like this.

@jennablackwell
Copy link

I'm experiencing the same crash with MediaElement. Navigating out of page with player causes a crash.

@Saturn
Copy link

Saturn commented Apr 9, 2020

I would also like to know if there is a quick and easy workaround for the time being.

@Saturn
Copy link

Saturn commented Apr 9, 2020

I used @StevenGranados's fix #9531 and still get exception.

Managed Stacktrace

	  at <unknown> <0xffffffff>
	  at ObjCRuntime.Messaging:void_objc_msgSend_IntPtr <0x00007>
	  at AVFoundation.AVPlayer:ReplaceCurrentItemWithPlayerItem <0x0005b>
	  at Xamarin.Forms.Platform.iOS.MediaElementRenderer:Dispose <0x002b7>
	  at Foundation.NSObject:Dispose <0x00023>

I then did

		protected override void Dispose(bool disposing)
		{
			base.Dispose(disposing);
                        return;
                        ///... rest of MediaElementRenderer disposal
                        ///...
                }

just to make sure it doesn't crash anymore which it doesn't.

My steps to reproduce were

  • Push new page with MediaElement. Click play on video.
  • Click back to pop page.
  • Page does pop but UI becomes unresponsive.

I notice these are slightly different than @StevenGranados.

@StevenGranados
Copy link
Contributor Author

I'm currently using Octane.Xam.VideoPlayer. That's been working well for me for the time being.

@code-hatchery
Copy link

This change is fixing the crashes on back button for me. In the iOS MediaElementRenderer.cs change this:
AVPlayerViewController _avPlayerViewController = new AVPlayerViewController();

to this:
AVPlayerViewController _avPlayerViewController = new AVPlayerViewController() { Player = new AVPlayer() };

I did not apply the patch already linked to this bug.

@code-hatchery
Copy link

Ah, I see that the effect of that change I gave was to prevent the setup of the _rateObserver. So the real fix is in the dispose method by changing this:
_rateObserver.Dispose();

to this:
_avPlayerViewController?.Player?.RemoveObserver(_rateObserver, "rate");

@Saturn
Copy link

Saturn commented Apr 18, 2020

Cool @code-hatchery. I will try that fix.

@Saturn
Copy link

Saturn commented Apr 19, 2020

Ah, I see that the effect of that change I gave was to prevent the setup of the _rateObserver. So the real fix is in the dispose method by changing this:
_rateObserver.Dispose();

to this:
_avPlayerViewController?.Player?.RemoveObserver(_rateObserver, "rate");

Yes, this fix works nicely for me. 👍

@brminnick
Copy link
Contributor

brminnick commented Apr 24, 2020

I confirmed that this bug happens when MediaElement is disposed.

When Dispose is called on Xamarin.Forms.Platform.iOS.MediaElementRenderer, a SIGSEVexception is thrown when _avPlayerViewController?.Player?.Pause(); is called.

Reproduction Steps

  1. Download/clone this repository: https://github.com/brminnick/MediaElementRepro/
  2. In Visual Studio, open MediaElementRepro.sln
  3. Build/Deploy MediaElementRepro.iOS to an iOS Simulator or Device
  4. In the iOS app, verify the video is playing
  5. In the iOS app, tap the Open New Media Element Page Button
  6. In the iOS app, verify the video is playing
  7. In the iOS app, tap the Close button
  8. In Visual Studio, confirm a SIGSEV exception is thrown
  9. If no exception has been thrown, repeat Steps 6-8
  • Note: Despite the crash, the video may continue to play on the iOS device. We can confirm the app has crashed by getting no response when tapping the Open New Media Element Page Button

Stack Trace

 at <unknown> <0xffffffff>
	  at ObjCRuntime.Messaging:void_objc_msgSend <0x000e9>
	  at AVFoundation.AVPlayer:Pause <0x00122>
	  at Xamarin.Forms.Platform.iOS.MediaElementRenderer:Dispose <0x00253>
	  at Foundation.NSObject:Dispose <0x00074>
	  at Xamarin.Forms.Platform.iOS.VisualElementPackager:Dispose <0x002c5>
	  at Xamarin.Forms.Platform.iOS.VisualElementPackager:Dispose <0x00071>
	  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1:Dispose <0x0024a>
	  at Foundation.NSObject:Dispose <0x00074>
	  at Xamarin.Forms.Platform.iOS.VisualElementPackager:Dispose <0x002c5>
	  at Xamarin.Forms.Platform.iOS.VisualElementPackager:Dispose <0x00071>
	  at Xamarin.Forms.Platform.iOS.PageRenderer:Dispose <0x0041a>
	  at Foundation.NSObject:Dispose <0x00074>
	  at Xamarin.Forms.Platform.iOS.DisposeHelpers:DisposeModalAndChildRenderers <0x0030f>
	  at <Xamarin-Forms-INavigation-PopModalAsync>d__25:MoveNext <0x008d2>
	  at MoveNextRunner:InvokeMoveNext <0x000ab>
	  at System.Threading.ExecutionContext:RunInternal <0x00585>
	  at System.Threading.ExecutionContext:Run <0x00092>
	  at MoveNextRunner:Run <0x001aa>
	  at System.Threading.Tasks.AwaitTaskContinuation:InvokeAction <0x00093>
	  at System.Threading.Tasks.AwaitTaskContinuation:RunCallback <0x00153>
	  at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation:Run <0x0013a>
	  at System.Threading.Tasks.Task:FinishContinuations <0x006a6>
	  at System.Threading.Tasks.Task:FinishStageThree <0x0015a>
	  at System.Threading.Tasks.Task`1:TrySetResult <0x0022a>
	  at System.Threading.Tasks.TaskCompletionSource`1:TrySetResult <0x00092>
	  at System.Threading.Tasks.TaskCompletionSource`1:SetResult <0x0007a>
	  at <>c__DisplayClass33_0:<DismissViewControllerAsync>b__0 <0x00082>
	  at SDAction:Invoke <0x00154>
	  at SDAction:Invoke <0x0010a>
	  at <unknown> <0xffffffff>
	  at UIKit.UIApplication:UIApplicationMain <0x00259>
	  at UIKit.UIApplication:Main <0x000b2>
	  at UIKit.UIApplication:Main <0x00132>
	  at MediaElementRepro.iOS.Application:Main <0x0007a>
	  at <Module>:runtime_invoke_void_object <0x00198>

https://github.com/brminnick/MediaElementRepro/

brminnick added a commit to brminnick/GitTrends that referenced this issue Apr 24, 2020
@Saturn
Copy link

Saturn commented Apr 25, 2020

I have also observed the video continuing to play but UI becomes totally unresponsive,.

@jefffhaynes
Copy link

Any update on this? Bit of a blocking bug for us.

@Az6bcn
Copy link

Az6bcn commented May 9, 2020

Any updates on this bug? I'm having the same issue on ios when I click back to go to the previous page, the UI becomes unresponsive.

@Saturn
Copy link

Saturn commented May 9, 2020

@Az6bcn Have you tried

Changing
_rateObserver.Dispose();

to this:
_avPlayerViewController?.Player?.RemoveObserver(_rateObserver, "rate");

in the MediaElement renderer for iOS?

@rodhemphill
Copy link

I too am blocked with this @samhouts .

@Saturn, what do you mean "changing the MediaElement renderer"? Are you suggesting there is a way to get the repo for just the MediaElement and we add that to our project? I thought you could get the repo for all of Forms or none. Or am I missing what you mean with your comment to @Az6bcn?

I also note that @brminnick 's repo has been removed.

@Saturn
Copy link

Saturn commented May 18, 2020

Hi @rodhemphill ,

I copied the MediaElementRenderer from https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.iOS/Renderers/MediaElementRenderer.cs

Added it to my Xamarin iOS project, changed the class name to MediaElementRenderer2 and added

[assembly: ExportRenderer(typeof(MediaElement), typeof(MediaElementRenderer2))]

above namespace.

You possibly do not need to change name of class.

I then just changed the Dispose method slightly based on @code-hatchery suggestion

_rateObserver.Dispose();

to this:

_avPlayerViewController?.Player?.RemoveObserver(_rateObserver, "rate");

This allowed me to use the MediaElement on iOS without the crashing issue. I really would hope the fix goes in sooner rather than later because this is just a quick workaround that worked for me.

@brminnick
Copy link
Contributor

@rodhemphill Thanks for the heads up! The repro link should work now: https://github.com/brminnick/MediaElementRepro

@npagare
Copy link

npagare commented Jul 28, 2020

Hi All,

In iOS version of my app, when I navigate back from a page that has MediaManager, I get an error in the output which is indicating that the dispose is not called. As a result my app becomes totally non responsive.

Do we have to call Dispose method on MediaManager explicitly in iOS Platform project or in Shared project ? [ this is not an issue with Android ]

iOS[57831:4538755] Warning: observer object was not disposed manually with Dispose()

=================================================================

Basic Fault Address Reporting

Memory around native instruction pointer (0x7fff512525f1):0x7fff512525e1 08 5b 41 5e 41 5f 5d c3 48 85 ff 7e 09 48 8b 07 .[A^A_].H..~.H..
0x7fff512525f1 8a 40 1c 24 01 c3 31 c0 c3 48 85 ff 74 17 78 1d .@.$..1..H.
.t.x.
0x7fff51252601 48 8b 07 48 85 c0 74 0d 48 89 c7 be 01 00 00 00 H..H..t.H.......
0x7fff51252611 e9 a0 d4 00 00 48 8d 05 5b 1c 02 00 c3 48 89 f8 .....H..[....H..

=================================================================

Managed Stacktrace:

at <0xffffffff>
at ObjCRuntime.Messaging:void_objc_msgSend <0x000e1>
at AVFoundation.AVPlayer:Pause <0x00122>
at Xamarin.Forms.Platform.iOS.MediaElementRenderer:Dispose <0x00327>

@npagare
Copy link

npagare commented Jul 29, 2020

Hi @samhouts - is the fix for this issue going to be packaged with XF 4.8 Pre 3 ?

@thisisthekap
Copy link
Contributor

@samhouts Please reopen this bug because the fix for it was reverted in #11862

@dalinet
Copy link

dalinet commented Oct 27, 2020

Hi @rodhemphill ,

I copied the MediaElementRenderer from https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.iOS/Renderers/MediaElementRenderer.cs

Added it to my Xamarin iOS project, changed the class name to MediaElementRenderer2 and added

[assembly: ExportRenderer(typeof(MediaElement), typeof(MediaElementRenderer2))]

above namespace.

You possibly do not need to change name of class.

I then just changed the Dispose method slightly based on @code-hatchery suggestion

_rateObserver.Dispose();

to this:

_avPlayerViewController?.Player?.RemoveObserver(_rateObserver, "rate");

This allowed me to use the MediaElement on iOS without the crashing issue. I really would hope the fix goes in sooner rather than later because this is just a quick workaround that worked for me.

Incredible solution, Thanks Friend!!!!!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/mediaelement e/2 🕑 2 i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often in-progress This issue has an associated pull request that may resolve it! p/iOS 🍎 t/bug 🐛
Projects
None yet