Maui icon indicating copy to clipboard operation
Maui copied to clipboard

[BUG] Crash on iOS when using MediaElement inside a CarouselView

Open dan3988 opened this issue 1 year ago • 4 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

  • [x] I have read the "Reporting a bug" section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug

Current Behavior

When using a MediaElement inside a carousel view, the app will crash on iOS with the following message:

child view controller:<AVPlayerViewController: 0x104008800> should have parent view controller:<Microsoft_Maui_Controls_Handlers_Items_CarouselViewController: 0x102777b80> but actual parent is:<Microsoft_Maui_Platform_PageViewController: 0x102774910>

Full stack trace:

Unhandled Exception:
ObjCRuntime.ObjCException: Objective-C exception thrown.  Name: UIViewControllerHierarchyInconsistency Reason: child view controller:<AVPlayerViewController: 0x104008800> should have parent view controller:<Microsoft_Maui_Controls_Handlers_Items_CarouselViewController: 0x102777b80> but actual parent is:<Microsoft_Maui_Platform_PageViewController: 0x102774910>
Native stack trace:
	0   CoreFoundation                      0x0000000180491128 __exceptionPreprocess + 172
	1   libobjc.A.dylib                     0x000000018008412c objc_exception_throw + 56
	2   CoreFoundation                      0x0000000180491038 -[NSException initWithCoder:] + 0
	3   UIKitCore                           0x00000001857f3ae4 -[UIView(Hierarchy) _associatedViewControllerForwardsAppearanceCallbacks:performHierarchyCheck:isRoot:] + 216
	4   UIKitCore                           0x00000001857f4250 -[UIView(Hierarchy) _willMoveToWindow:withAncestorView:] + 184
	5   UIKitCore                           0x00000001857f4d18 _makeSubTreePerformSelector + 428
	6   UIKitCore                           0x00000001857f4d54 _makeSubTreePerformSelector + 488
	7   UIKitCore                           0x00000001857f4d54 _makeSubTreePerformSelector + 488
	8   UIKitCore                           0x00000001858016a0 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 384
	9   UIKitCore                           0x0000000184a39e3c -[UICollectionView _addSubview:positioned:relativeTo:] + 144
	10  UIKitCore                           0x00000001854bc098 -[_UICollectionViewSubviewRouter addControlledSubview:zIndex:orderMode:] + 816
	11  UIKitCore                           0x0000000184a39a34 -[UICollectionView _addControlledSubview:atZIndex:forced:initialAttributes:] + 300
	12  UIKitCore                           0x0000000184a3e038 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 1992
	13  UIKitCore                           0x0000000184a46780 -[UICollectionView _createVisibleViewsForSingleCategoryAttributes:limitCreation:fadeForBoundsChange:] + 1064
	14  UIKitCore                           0x0000000184a46a9c -[UICollectionView _createVisibleViewsForAttributes:fadeForBoundsChange:notifyLayoutForVisibleCellsPass:] + 288
	15  UIKitCore                           0x0000000184a44e90 -[UICollectionView _updateVisibleCellsNow:] + 3180
	16  UIKitCore                           0x0000000184a49ec4 -[UICollectionView layoutSubviews] + 300
	17  UIKitCore                           0x0000000185806d78 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1492
	18  QuartzCore                          0x0000000189ffa5b0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 440
	19  QuartzCore                          0x000000018a005368 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 124
	20  QuartzCore                          0x0000000189f20520 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 460
	21  QuartzCore                          0x0000000189f4fcc0 _ZN2CA11Transaction6commitEv + 652
	22  UIKitCore                           0x0000000185308804 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 32
	23  CoreFoundation                      0x00000001803f1ce0 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
	24  CoreFoundation                      0x00000001803f1428 __CFRunLoopDoBlocks + 352
	25  CoreFoundation                      0x00000001803ebccc __CFRunLoopRun + 788
	26  CoreFoundation                      0x00000001803eb5a4 CFRunLoopRunSpecific + 572
	27  GraphicsServices                    0x000000018e9fbae4 GSEventRunModal + 160
	28  UIKitCore                           0x00000001852f02e4 -[UIApplication _run] + 868
	29  UIKitCore                           0x00000001852f3f5c UIApplicationMain + 124
	30  libxamarin-dotnet-debug.dylib       0x000000010245bf44 xamarin_UIApplicationMain + 60
	31  libmonosgen-2.0.dylib               0x0000000102b8ca50 do_icall + 316
	32  libmonosgen-2.0.dylib               0x0000000102b8b2f0 do_icall_wrapper + 356
	33  libmonosgen-2.0.dylib               0x0000000102b7fd3c mono_interp_exec_method + 2784
	34  libmonosgen-2.0.dylib               0x0000000102b7d910 interp_runtime_invoke + 244
	35  libmonosgen-2.0.dylib               0x0000000102a969b0 mono_jit_runtime_invoke + 1116
	36  libmonosgen-2.0.dylib               0x0000000102c51a14 mono_runtime_invoke_checked + 148
	37  libmonosgen-2.0.dylib               0x0000000102c58ff4 mono_runtime_exec_main_checked + 116
	38  libmonosgen-2.0.dylib               0x0000000102aeac70 mono_jit_exec + 364
	39  libxamarin-dotnet-debug.dylib       0x000000010249fb00 xamarin_main + 2320
	40  MediaElementCarouselBug             0x0000000100b5ab44 main + 72
	41  dyld                                0x00000001015cd544 start_sim + 20
	42  ???                                 0x00000001010ca0e0 0x0 + 4312572128
	43  ???                                 0x5a5a000000000000 0x0 + 6510516211317473280

   at ObjCRuntime.Runtime.ThrowException(IntPtr gchandle) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:line 2594
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 60
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 94
   at MediaElementCarouselBug.Program.Main(String[] args) in F:\Source\experiments\MediaElementCarouselBug\Platforms\iOS\Program.cs:line 14
Native stack trace:
	0   CoreFoundation                      0x0000000180491128 __exceptionPreprocess + 172
	1   libobjc.A.dylib                     0x000000018008412c objc_exception_throw + 56
	2   CoreFoundation                      0x0000000180491038 -[NSException initWithCoder:] + 0
	3   UIKitCore                           0x00000001857f3ae4 -[UIView(Hierarchy) _associatedViewControllerForwardsAppearanceCallbacks:performHierarchyCheck:isRoot:] + 216
	4   UIKitCore                           0x00000001857f4250 -[UIView(Hierarchy) _willMoveToWindow:withAncestorView:] + 184
	5   UIKitCore                           0x00000001857f4d18 _makeSubTreePerformSelector + 428
	6   UIKitCore                           0x00000001857f4d54 _makeSubTreePerformSelector + 488
	7   UIKitCore                           0x00000001857f4d54 _makeSubTreePerformSelector + 488
	8   UIKitCore                           0x00000001858016a0 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 384
	9   UIKitCore                           0x0000000184a39e3c -[UICollectionView _addSubview:positioned:relativeTo:] + 144
	10  UIKitCore                           0x00000001854bc098 -[_UICollectionViewSubviewRouter addControlledSubview:zIndex:orderMode:] + 816
	11  UIKitCore                           0x0000000184a39a34 -[UICollectionView _addControlledSubview:atZIndex:forced:initialAttributes:] + 300
	12  UIKitCore                           0x0000000184a3e038 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 1992
	13  UIKitCore                           0x0000000184a46780 -[UICollectionView _createVisibleViewsForSingleCategoryAttributes:limitCreation:fadeForBoundsChange:] + 1064
	14  UIKitCore                           0x0000000184a46a9c -[UICollectionView _createVisibleViewsForAttributes:fadeForBoundsChange:notifyLayoutForVisibleCellsPass:] + 288
	15  UIKitCore                           0x0000000184a44e90 -[UICollectionView _updateVisibleCellsNow:] + 3180
	16  UIKitCore                           0x0000000184a49ec4 -[UICollectionView layoutSubviews] + 300
	17  UIKitCore                           0x0000000185806d78 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1492
	18  QuartzCore                          0x0000000189ffa5b0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 440
	19  QuartzCore                          0x000000018a005368 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 124
	20  QuartzCore                          0x0000000189f20520 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 460
	21  QuartzCore                          0x0000000189f4fcc0 _ZN2CA11Transaction6commitEv + 652
	22  UIKitCore                           0x0000000185308804 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 32
	23  CoreFoundation                      0x00000001803f1ce0 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
	24  CoreFoundation                      0x00000001803f1428 __CFRunLoopDoBlocks + 352
	25  CoreFoundation                      0x00000001803ebccc __CFRunLoopRun + 788
	26  CoreFoundation                      0x00000001803eb5a4 CFRunLoopRunSpecific + 572
	27  GraphicsServices                    0x000000018e9fbae4 GSEventRunModal + 160
	28  UIKitCore                           0x00000001852f02e4 -[UIApplication _run] + 868
	29  UIKitCore                           0x00000001852f3f5c UIApplicationMain + 124
	30  libxamarin-dotnet-debug.dylib       0x000000010245bf44 xamarin_UIApplicationMain + 60
	31  libmonosgen-2.0.dylib               0x0000000102b8ca50 do_icall + 316
	32  libmonosgen-2.0.dylib               0x0000000102b8b2f0 do_icall_wrapper + 356
	33  libmonosgen-2.0.dylib               0x0000000102b7fd3c mono_interp_exec_method + 2784
	34  libmonosgen-2.0.dylib               0x0000000102b7d910 interp_runtime_invoke + 244
	35  libmonosgen-2.0.dylib               0x0000000102a969b0 mono_jit_runtime_invoke + 1116
	36  libmonosgen-2.0.dylib               0x0000000102c51a14 mono_runtime_invoke_checked + 148
	37  libmonosgen-2.0.dylib               0x0000000102c58ff4 mono_runtime_exec_main_checked + 116
	38  libmonosgen-2.0.dylib               0x0000000102aeac70 mono_jit_exec + 364
	39  libxamarin-dotnet-debug.dylib       0x000000010249fb00 xamarin_main + 2320
	40  MediaElementCarouselBug             0x0000000100b5ab44 main + 72
	41  dyld                                0x00000001015cd544 start_sim + 20
	42  ???                                 0x00000001010ca0e0 0x0 + 4312572128
	43  ???                                 0x5a5a000000000000 0x0 + 6510516211317473280
2024-02-29 10:20:42.258618+0000 MediaElementCarouselBug[5811:1634581] Unhandled managed exception: Objective-C exception thrown.  Name: UIViewControllerHierarchyInconsistency Reason: child view controller:<AVPlayerViewController: 0x104008800> should have parent view controller:<Microsoft_Maui_Controls_Handlers_Items_CarouselViewController: 0x102777b80> but actual parent is:<Microsoft_Maui_Platform_PageViewController: 0x102774910>
Native stack trace:
	0   CoreFoundation                      0x0000000180491128 __exceptionPreprocess + 172
	1   libobjc.A.dylib                     0x000000018008412c objc_exception_throw + 56
	2   CoreFoundation                      0x0000000180491038 -[NSException initWithCoder:] + 0
	3   UIKitCore                           0x00000001857f3ae4 -[UIView(Hierarchy) _associatedViewControllerForwardsAppearanceCallbacks:performHierarchyCheck:isRoot:] + 216
	4   UIKitCore                           0x00000001857f4250 -[UIView(Hierarchy) _willMoveToWindow:withAncestorView:] + 184
	5   UIKitCore                           0x00000001857f4d18 _makeSubTreePerformSelector + 428
	6   UIKitCore                           0x00000001857f4d54 _makeSubTreePerformSelector + 488
	7   UIKitCore                           0x00000001857f4d54 _makeSubTreePerformSelector + 488
	8   UIKitCore                           0x00000001858016a0 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 384
	9   UIKitCore                           0x0000000184a39e3c -[UICollectionView _addSubview:positioned:relativeTo:] + 144
	10  UIKitCore                           0x00000001854bc098 -[_UICollectionViewSubviewRouter addControlledSubview:zIndex:orderMode:] + 816
	11  UIKitCore                           0x0000000184a39a34 -[UICollectionView _addControlledSubview:atZIndex:forced:initialAttributes:] + 300
	12  UIKitCore                           0x0000000184a3e038 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 1992
	13  UIKitCore                           0x0000000184a46780 -[UICollectionView _createVisibleViewsForSingleCategoryAttributes:limitCreation:fadeForBoundsChange:] + 1064
	14  UIKitCore                           0x0000000184a46a9c -[UICollectionView _createVisibleViewsForAttributes:fadeForBoundsChange:notifyLayoutForVisibleCellsPass:] + 288
	15  UIKitCore                           0x0000000184a44e90 -[UICollectionView _updateVisibleCellsNow:] + 3180
	16  UIKitCore                           0x0000000184a49ec4 -[UICollectionView layoutSubviews] + 300
	17  UIKitCore                           0x0000000185806d78 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1492
	18  QuartzCore                          0x0000000189ffa5b0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 440
	19  QuartzCore                          0x000000018a005368 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 124
	20  QuartzCore                          0x0000000189f20520 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 460
	21  QuartzCore                          0x0000000189f4fcc0 _ZN2CA11Transaction6commitEv + 652
	22  UIKitCore                           0x0000000185308804 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 32
	23  CoreFoundation                      0x00000001803f1ce0 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
	24  CoreFoundation                      0x00000001803f1428 __CFRunLoopDoBlocks + 352
	25  CoreFoundation                      0x00000001803ebccc __CFRunLoopRun + 788
	26  CoreFoundation                      0x00000001803eb5a4 CFRunLoopRunSpecific + 572
	27  GraphicsServices                    0x000000018e9fbae4 GSEventRunModal + 160
	28  UIKitCore                           0x00000001852f02e4 -[UIApplication _run] + 868
	29  UIKitCore                           0x00000001852f3f5c UIApplicationMain + 124
	30  libxamarin-dotnet-debug.dylib       0x000000010245bf44 xamarin_UIApplicationMain + 60
	31  libmonosgen-2.0.dylib               0x0000000102b8ca50 do_icall + 316
	32  libmonosgen-2.0.dylib               0x0000000102b8b2f0 do_icall_wrapper + 356
	33  libmonosgen-2.0.dylib               0x0000000102b7fd3c mono_interp_exec_method + 2784
	34  libmonosgen-2.0.dylib               0x0000000102b7d910 interp_runtime_invoke + 244
	35  libmonosgen-2.0.dylib               0x0000000102a969b0 mono_jit_runtime_invoke + 1116
	36  libmonosgen-2.0.dylib               0x0000000102c51a14 mono_runtime_invoke_checked + 148
	37  libmonosgen-2.0.dylib               0x0000000102c58ff4 mono_runtime_exec_main_checked + 116
	38  libmonosgen-2.0.dylib               0x0000000102aeac70 mono_jit_exec + 364
	39  libxamarin-dotnet-debug.dylib       0x000000010249fb00 xamarin_main + 2320
	40  MediaElementCarouselBug             0x0000000100b5ab44 main + 72
	41  dyld                                0x00000001015cd544 start_sim + 20
	42  ???                                 0x00000001010ca0e0 0x0 + 4312572128
	43  ???                                 0x5a5a000000000000 0x0 + 6510516211317473280
 (ObjCRuntime.ObjCException)
   at ObjCRuntime.Runtime.ThrowException(IntPtr gchandle) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:line 2594
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 60
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 94
   at MediaElementCarouselBug.Program.Main(String[] args) in F:\Source\experiments\MediaElementCarouselBug\Platforms\iOS\Program.cs:line 14

=================================================================
	Native Crash Reporting
=================================================================
Got a SIGABRT 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:
=================================================================
	0x102b7c1a0 - /Users/onewizard/Library/Developer/CoreSimulator/Devices/6032833E-9302-4B57-9D8E-460CBD3073E5/data/Containers/Bundle/Application/6841077E-2E44-437A-9E43-2CC53F2465BE/MediaElementCarouselBug.app/libmonosgen-2.0.dylib : mono_dump_native_crash_info
	0x102b2c1c0 - /Users/onewizard/Library/Developer/CoreSimulator/Devices/6032833E-9302-4B57-9D8E-460CBD3073E5/data/Containers/Bundle/Application/6841077E-2E44-437A-9E43-2CC53F2465BE/MediaElementCarouselBug.app/libmonosgen-2.0.dylib : mono_handle_native_crash
	0x102e4ebc8 - /Users/onewizard/Library/Developer/CoreSimulator/Devices/6032833E-9302-4B57-9D8E-460CBD3073E5/data/Containers/Bundle/Application/6841077E-2E44-437A-9E43-2CC53F2465BE/MediaElementCarouselBug.app/libmonosgen-2.0.dylib : sigabrt_signal_handler.cold.1
	0x102b7ba60 - /Users/onewizard/Library/Developer/CoreSimulator/Devices/6032833E-9302-4B57-9D8E-460CBD3073E5/data/Containers/Bundle/Application/6841077E-2E44-437A-9E43-2CC53F2465BE/MediaElementCarouselBug.app/libmonosgen-2.0.dylib : mono_runtime_setup_stat_profiler
	0x100ffbc60 - /usr/lib/system/libsystem_platform.dylib : _sigtramp
	0x101edb1d0 - /usr/lib/system/libsystem_pthread.dylib : pthread_kill
	0x1801605cc - /Library/Developer/CoreSimulator/Volumes/iOS_21C62/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 17.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libsystem_c.dylib : abort
	0x10248b098 - /Users/onewizard/Library/Developer/CoreSimulator/Devices/6032833E-9302-4B57-9D8E-460CBD3073E5/data/Containers/Bundle/Application/6841077E-2E44-437A-9E43-2CC53F2465BE/MediaElementCarouselBug.app/libxamarin-dotnet-debug.dylib : xamarin_find_protocol_wrapper_type
	0x102c09150 - /Users/onewizard/Library/Developer/CoreSimulator/Devices/6032833E-9302-4B57-9D8E-460CBD3073E5/data/Containers/Bundle/Application/6841077E-2E44-437A-9E43-2CC53F2465BE/MediaElementCarouselBug.app/libmonosgen-2.0.dylib : mono_invoke_unhandled_exception_hook
	0x102aeac98 - /Users/onewizard/Library/Developer/CoreSimulator/Devices/6032833E-9302-4B57-9D8E-460CBD3073E5/data/Containers/Bundle/Application/6841077E-2E44-437A-9E43-2CC53F2465BE/MediaElementCarouselBug.app/libmonosgen-2.0.dylib : mono_jit_exec
	0x10249fb00 - /Users/onewizard/Library/Developer/CoreSimulator/Devices/6032833E-9302-4B57-9D8E-460CBD3073E5/data/Containers/Bundle/Application/6841077E-2E44-437A-9E43-2CC53F2465BE/MediaElementCarouselBug.app/libxamarin-dotnet-debug.dylib : xamarin_main
	0x100b5ab44 - /Users/onewizard/Library/Developer/CoreSimulator/Devices/6032833E-9302-4B57-9D8E-460CBD3073E5/data/Containers/Bundle/Application/6841077E-2E44-437A-9E43-2CC53F2465BE/MediaElementCarouselBug.app/MediaElementCarouselBug : main
	0x1015cd544 - Unknown
	0x1010ca0e0 - Unknown
	0x5a5a000000000000 - Unknown

=================================================================
	Basic Fault Address Reporting
=================================================================
Memory around native instruction pointer (0x1020889ec):0x1020889dc  c0 03 5f d6 c0 03 5f d6 10 29 80 d2 
01 10 00 d4  .._..._..)......
0x1020889ec  e3 00 00 54 fd 7b bf a9 fd 03 
00 91 ce e2 ff 97  ...T.{..........
0x1020889fc  bf 03 00 91 fd 7b c1 
a8 c0 03 5f d6 c0 03 5f d6  .....{...._..._.
0x102088a0c  
70 0a 80 d2 01 10 00 d4 e3 00 00 54 fd 7b bf a9  p........
..T.{..

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

Expected Behavior

The app should not crash

Steps To Reproduce

  1. Create a .NET MAUI app
  2. Add a page with a CarouselView with a MediaElement in the ItemTemplate
  3. Add one or more sources to the ItemsSource of the CarouselView
  4. Run the app on iOS and open the page

Link to public reproduction project repository

https://github.com/dan3988/MediaElementCarouselBug

Environment

- CommunityToolkit.Maui.MediaElement: 3.1.1
- OS: iOS 17.4
- .NET MAUI: 8.0

Anything else?

No response

dan3988 avatar May 28 '24 07:05 dan3988

im facing same issue

Pmr-precure avatar Jun 06 '24 10:06 Pmr-precure

I was able to verify this is an issue @vhugogarcia

ne0rrmatrix avatar Jun 18 '24 03:06 ne0rrmatrix

This also occurs in a ListView or CollectionView

matthewyoungias avatar Jun 19 '24 12:06 matthewyoungias

Update:

My issue turned out to be in my code. I had create a custom PageHandler that I had set to inherit from ContentViewHandler instead of PageHandler. This caused the cast to PageHandler in the MediaElementHandler to fail so it would have to relay on WindowStateManager.Default.GetCurrentUIViewController() to get the correct ViewController, but that turned out to be unreliable. Just though I've post this update in case somebody else has the same issue.


I'm using the MediaElement in a presentation application and I see this issue when the MediaElement is first added, then removed, and then added again. This typically happens when the user toggle the presentation mode on, off, and back on again.

This is how I create the MediaElement:

ExternalVideoPlayer = new MediaElement()
{
    Aspect = Aspect.AspectFit,
    ShouldAutoPlay = false,
    ShouldShowPlaybackControls = false,
    Source = LocalVideoPlayer.Source
};

try
{
    externalView.Children.Add(ExternalVideoPlayer);
}
catch (Exception ex)
{
    Debug.WriteLine($"Error adding video player to external view: {ex.Message}");
}

And this is how I remove it:

if (externalView != null && externalView.Children.Contains(ExternalVideoPlayer))
{
    externalView.Children.Remove(ExternalVideoPlayer);
}
ExternalVideoPlayer.Parent = null;
ExternalVideoPlayer.Handler?.DisconnectHandler();
ExternalVideoPlayer = null;

torfluor avatar Jul 03 '24 10:07 torfluor

In his last comment @torfluor mentions that there was an issue in his code causing a problem (thanks for pointing this out!). Now, just to be clear: The original issue is still reproducible with .NET MAUI 8.0.71 / CommunityToolkit.Maui.MediaElement 4.0.1.

To verify this I created another minimal sample app: https://github.com/AndreasHennig/MediaElementSampleApp/tree/main

It contains a ContentPage with a CarouselView containing a MediaElement as it's DateTemplate:

<CarouselView>
    <CarouselView.ItemsSource>
        <x:Array Type="{x:Type x:String}">
            <x:String>https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4</x:String>
            <x:String>https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/Sintel.mp4</x:String>
            <x:String>https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4</x:String>
        </x:Array>
    </CarouselView.ItemsSource>
    <CarouselView.ItemTemplate>
        <DataTemplate>
            <toolkit:MediaElement
                Source="{Binding .}"
                ShouldShowPlaybackControls="True" />
        </DataTemplate>
    </CarouselView.ItemTemplate>
</CarouselView>

On Android, this sample app works perfectly fine. I tested it on an Android Emulator with Android 14.

However, on iOS the sample app crashes right after the start with the stack trace already mentioned:

Show stack trace
ObjCRuntime.ObjCException: Objective-C exception thrown.  Name: UIViewControllerHierarchyInconsistency Reason: child view controller:<AVPlayerViewController: 0x106a97200> should have parent view controller:<Microsoft_Maui_Controls_Handlers_Items_CarouselViewController: 0x10ac0ab00> but actual parent is:<Microsoft_Maui_Controls_Platform_Compatibility_ShellFlyoutRenderer: 0x104131bb0>
Native stack trace:
	0   CoreFoundation                      0x00000001804ae138 __exceptionPreprocess + 172
	1   libobjc.A.dylib                     0x0000000180087db4 objc_exception_throw + 56
	2   CoreFoundation                      0x00000001804ae048 -[NSException initWithCoder:] + 0
	3   UIKitCore                           0x00000001858f6e54 -[UIView(Hierarchy) _associatedViewControllerForwardsAppearanceCallbacks:performHierarchyCheck:isRoot:] + 216
	4   UIKitCore                           0x00000001858f75dc -[UIView(Hierarchy) _willMoveToWindow:withAncestorView:] + 184
	5   UIKitCore                           0x00000001858f80a4 _makeSubTreePerformSelector + 428
	6   UIKitCore                           0x00000001858f80e0 _makeSubTreePerformSelector + 488
	7   UIKitCore                           0x00000001858f80e0 _makeSubTreePerformSelector + 488
	8   UIKitCore                           0x0000000185904a48 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 384
	9   UIKitCore                           0x0000000184b23934 -[UICollectionView _addSubview:positioned:relativeTo:] + 144
	10  UIKitCore                           0x00000001855b96c8 -[_UICollectionViewSubviewRouter addControlledSubview:zIndex:orderMode:] + 816
	11  UIKitCore                           0x0000000184b2352c -[UICollectionView _addControlledSubview:atZIndex:forced:initialAttributes:] + 300
	12  UIKitCore                           0x0000000184b27af4 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 1992
	13  UIKitCore                           0x0000000184b30304 -[UICollectionView _createVisibleViewsForSingleCategoryAttributes:limitCreation:fadeForBoundsChange:] + 1060
	14  UIKitCore                           0x0000000184b30620 -[UICollectionView _createVisibleViewsForAttributes:fadeForBoundsChange:notifyLayoutForVisibleCellsPass:] + 288
	15  UIKitCore                           0x0000000184b2ea18 -[UICollectionView _updateVisibleCellsNow:] + 3180
	16  UIKitCore                           0x0000000184b33a50 -[UICollectionView layoutSubviews] + 300
	17  UIKitCore                           0x000000018590a128 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1492
	18  QuartzCore                          0x000000018a1ae654 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 440
	19  UIKitCore                           0x00000001858f95ec -[UIView(Hierarchy) layoutBelowIfNeeded] + 292
	20  UIKitCore                           0x0000000184c0dd00 -[UINavigationController _layoutViewController:] + 812
	21  UIKitCore                           0x0000000184c078fc -[UINavigationController _layoutTopViewControllerLookForNested:] + 416
	22  UIKitCore                           0x0000000184c0600c -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 876
	23  UIKitCore                           0x00000001858cb904 -[UINavigationTransitionView _notifyDelegateTransitionDidStopWithContext:] + 292
	24  UIKitCore                           0x00000001858cbb80 -[UINavigationTransitionView _cleanupTransition] + 548
	25  UIKitCore                           0x0000000185900f68 +[UIView(UIViewAnimationWithBlocks) conditionallyAnimate:withAnimation:layout:completion:] + 120
	26  UIKitCore                           0x00000001858cb604 -[UINavigationTransitionView transition:fromView:toView:] + 1440
	27  UIKitCore                           0x0000000184c0e994 -[UINavigationController _startTransition:fromViewController:toViewController:] + 2452
	28  UIKitCore                           0x0000000184c0ed68 -[UINavigationController _startDeferredTransitionIfNeeded:] + 604
	29  UIKitCore                           0x0000000184c0fc20 -[UINavigationController __viewWillLayoutSubviews] + 92
	30  UIKitCore                           0x0000000184bf672c -[UILayoutContainerView layoutSubviews] + 168
	31  UIKitCore                           0x000000018590a128 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1492
	32  QuartzCore                          0x000000018a1ae654 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 440
	33  QuartzCore                          0x000000018a1b940c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 124
	34  QuartzCore                          0x000000018a0f2d30 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 460
	35  QuartzCore                          0x000000018a122aa8 _ZN2CA11Transaction6commitEv + 652
	36  UIKitCore                           0x0000000185400570 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 32
	37  CoreFoundation                      0x000000018040ec50 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
	38  CoreFoundation                      0x000000018040e398 __CFRunLoopDoBlocks + 352
	39  CoreFoundation                      0x0000000180408c3c __CFRunLoopRun + 788
	40  CoreFoundation                      0x0000000180408514 CFRunLoopRunSpecific + 572
	41  GraphicsServices                    0x000000018ef06ae4 GSEventRunModal + 160
	42  UIKitCore                           0x00000001853e8040 -[UIApplication _run] + 868
	43  UIKitCore                           0x00000001853ebcc8 UIApplicationMain + 124
	44  libxamarin-dotnet-debug.dylib       0x0000000103bbbf00 xamarin_UIApplicationMain + 60
	45  libmonosgen-2.0.dylib               0x0000000104884cc4 do_icall + 316
	46  libmonosgen-2.0.dylib               0x0000000104883564 do_icall_wrapper + 356
	47  libmonosgen-2.0.dylib               0x0000000104877fb0 mono_interp_exec_method + 2784
	48  libmonosgen-2.0.dylib               0x0000000104875b84 interp_runtime_invoke + 244
	49  libmonosgen-2.0.dylib               0x000000010478ede8 mono_jit_runtime_invoke + 1116
	50  libmonosgen-2.0.dylib               0x0000000104949d20 mono_runtime_invoke_checked + 148
	51  libmonosgen-2.0.dylib               0x0000000104951300 mono_runtime_exec_main_checked + 116
	52  libmonosgen-2.0.dylib               0x00000001047e30b0 mono_jit_exec + 364
	53  libxamarin-dotnet-debug.dylib       0x0000000103bffb00 xamarin_main + 2320
	54  MediaElementSampleApp               0x00000001025deb44 main + 72
	55  dyld                                0x0000000102b59544 start_sim + 20
	56  ???                                 0x0000000102dc20e0 0x0 + 4342948064
	57  ???                                 0x266a000000000000 0x0 + 2768024920972591104

   at ObjCRuntime.Runtime.ThrowException(IntPtr gchandle) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:line 2594
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 60
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 94
   at MediaElementSampleApp.Program.Main(String[] args) in /Users/Hennig/gitRepos/MediaElementSampleApp/src/MediaElementSampleApp/Platforms/iOS/Program.cs:line 12
Native stack trace:
	0   CoreFoundation                      0x00000001804ae138 __exceptionPreprocess + 172
	1   libobjc.A.dylib                     0x0000000180087db4 objc_exception_throw + 56
	2   CoreFoundation                      0x00000001804ae048 -[NSException initWithCoder:] + 0
	3   UIKitCore                           0x00000001858f6e54 -[UIView(Hierarchy) _associatedViewControllerForwardsAppearanceCallbacks:performHierarchyCheck:isRoot:] + 216
	4   UIKitCore                           0x00000001858f75dc -[UIView(Hierarchy) _willMoveToWindow:withAncestorView:] + 184
	5   UIKitCore                           0x00000001858f80a4 _makeSubTreePerformSelector + 428
	6   UIKitCore                           0x00000001858f80e0 _makeSubTreePerformSelector + 488
	7   UIKitCore                           0x00000001858f80e0 _makeSubTreePerformSelector + 488
	8   UIKitCore                           0x0000000185904a48 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 384
	9   UIKitCore                           0x0000000184b23934 -[UICollectionView _addSubview:positioned:relativeTo:] + 144
	10  UIKitCore                           0x00000001855b96c8 -[_UICollectionViewSubviewRouter addControlledSubview:zIndex:orderMode:] + 816
	11  UIKitCore                           0x0000000184b2352c -[UICollectionView _addControlledSubview:atZIndex:forced:initialAttributes:] + 300
	12  UIKitCore                           0x0000000184b27af4 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 1992
	13  UIKitCore                           0x0000000184b30304 -[UICollectionView _createVisibleViewsForSingleCategoryAttributes:limitCreation:fadeForBoundsChange:] + 1060
	14  UIKitCore                           0x0000000184b30620 -[UICollectionView _createVisibleViewsForAttributes:fadeForBoundsChange:notifyLayoutForVisibleCellsPass:] + 288
	15  UIKitCore                           0x0000000184b2ea18 -[UICollectionView _updateVisibleCellsNow:] + 3180
	16  UIKitCore                           0x0000000184b33a50 -[UICollectionView layoutSubviews] + 300
	17  UIKitCore                           0x000000018590a128 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1492
	18  QuartzCore                          0x000000018a1ae654 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 440
	19  UIKitCore                           0x00000001858f95ec -[UIView(Hierarchy) layoutBelowIfNeeded] + 292
	20  UIKitCore                           0x0000000184c0dd00 -[UINavigationController _layoutViewController:] + 812
	21  UIKitCore                           0x0000000184c078fc -[UINavigationController _layoutTopViewControllerLookForNested:] + 416
	22  UIKitCore                           0x0000000184c0600c -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 876
	23  UIKitCore                           0x00000001858cb904 -[UINavigationTransitionView _notifyDelegateTransitionDidStopWithContext:] + 292
	24  UIKitCore                           0x00000001858cbb80 -[UINavigationTransitionView _cleanupTransition] + 548
	25  UIKitCore                           0x0000000185900f68 +[UIView(UIViewAnimationWithBlocks) conditionallyAnimate:withAnimation:layout:completion:] + 120
	26  UIKitCore                           0x00000001858cb604 -[UINavigationTransitionView transition:fromView:toView:] + 1440
	27  UIKitCore                           0x0000000184c0e994 -[UINavigationController _startTransition:fromViewController:toViewController:] + 2452
	28  UIKitCore                           0x0000000184c0ed68 -[UINavigationController _startDeferredTransitionIfNeeded:] + 604
	29  UIKitCore                           0x0000000184c0fc20 -[UINavigationController __viewWillLayoutSubviews] + 92
	30  UIKitCore                           0x0000000184bf672c -[UILayoutContainerView layoutSubviews] + 168
	31  UIKitCore                           0x000000018590a128 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1492
	32  QuartzCore                          0x000000018a1ae654 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 440
	33  QuartzCore                          0x000000018a1b940c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 124
	34  QuartzCore                          0x000000018a0f2d30 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 460
	35  QuartzCore                          0x000000018a122aa8 _ZN2CA11Transaction6commitEv + 652
	36  UIKitCore                           0x0000000185400570 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 32
	37  CoreFoundation                      0x000000018040ec50 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
	38  CoreFoundation                      0x000000018040e398 __CFRunLoopDoBlocks + 352
	39  CoreFoundation                      0x0000000180408c3c __CFRunLoopRun + 788
	40  CoreFoundation                      0x0000000180408514 CFRunLoopRunSpecific + 572
	41  GraphicsServices                    0x000000018ef06ae4 GSEventRunModal + 160
	42  UIKitCore                           0x00000001853e8040 -[UIApplication _run] + 868
	43  UIKitCore                           0x00000001853ebcc8 UIApplicationMain + 124
	44  libxamarin-dotnet-debug.dylib       0x0000000103bbbf00 xamarin_UIApplicationMain + 60
	45  libmonosgen-2.0.dylib               0x0000000104884cc4 do_icall + 316
	46  libmonosgen-2.0.dylib               0x0000000104883564 do_icall_wrapper + 356
	47  libmonosgen-2.0.dylib               0x0000000104877fb0 mono_interp_exec_method + 2784
	48  libmonosgen-2.0.dylib               0x0000000104875b84 interp_runtime_invoke + 244
	49  libmonosgen-2.0.dylib               0x000000010478ede8 mono_jit_runtime_invoke + 1116
	50  libmonosgen-2.0.dylib               0x0000000104949d20 mono_runtime_invoke_checked + 148
	51  libmonosgen-2.0.dylib               0x0000000104951300 mono_runtime_exec_main_checked + 116
	52  libmonosgen-2.0.dylib               0x00000001047e30b0 mono_jit_exec + 364
	53  libxamarin-dotnet-debug.dylib       0x0000000103bffb00 xamarin_main + 2320
	54  MediaElementSampleApp               0x00000001025deb44 main + 72
	55  dyld                                0x0000000102b59544 start_sim + 20
	56  ???                                 0x0000000102dc20e0 0x0 + 4342948064
	57  ???                                 0x266a000000000000 0x0 + 2768024920972591104

I tested it with a iPhone 15 simulator with iOS 17.4 and Xcode 15.3.0 installed.

@matthewyoungias mentions that the issue also seems to occur when using the MediaElement inside a ListView or a CollectionView. I can confirm this. My sample app contains (commented out) sections to verify this: https://github.com/AndreasHennig/MediaElementSampleApp/blob/main/src/MediaElementSampleApp/MainPage.xaml#L22

AndreasHennig avatar Aug 01 '24 09:08 AndreasHennig

Reopening as the crash is confirmed again on iOS 18 image

TheCodeTraveler avatar Oct 03 '24 17:10 TheCodeTraveler