Skip to content

[iOS] ObjCRuntime.RuntimeException in Microsoft.Maui.Platform.LayoutView during CollectionView layout changes on V2 #29599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
vikher opened this issue May 20, 2025 · 2 comments
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView collectionview-cv2 platform/ios s/needs-info Issue needs more info from the author s/no-repro Can no longer be reproduced on latest s/triaged Issue has been reviewed t/bug Something isn't working

Comments

@vikher
Copy link

vikher commented May 20, 2025

Description

A ObjCRuntime.RuntimeException occurs on iOS when interacting with the page, which uses CollectionView V2. The crash happens inconsistently, often after the layout resize. The error indicates a failure to marshal an Objective-C object of type Microsoft_Maui_Platform_LayoutView (address 0x170efa840) because the managed type Microsoft.Maui.Platform.LayoutView lacks a constructor accepting a NativeHandle parameter. This leads to a failure in accessing the Superview property during LayoutSubviews, likely due to invalid state references after changing the ItemsLayout in the CollectionView. The MAUI 9.0.70 release notes mention fixes for Header/Footer issues after ItemsLayout changes, which do not address this.

In my example, I have a Page with an Expander in the CollectionView.Header. When this view expands or collapses (e.g., on a tap), the header size changes. With CollectionView V1, the items shift down appropriately. With CollectionView V2 it crashes under two conditions: After leaving the page idle for some time and then resizing the header or when rapidly tapping the Expander multiple times in quick succession

Screen.Recording.2025-05-20.at.3.07.47.PM.mp4

Image

Steps to Reproduce

  • Create a .NET MAUI app with a CollectionView (V2) on a page
  • Configure the CollectionView with a dynamic ItemsLayout that changes (e.g., via pull-to-refresh or resize event).
  • Add a Header or Footer to the CollectionView.
  • Perform an action on the page to trigger layout resizing.
  • Observe the app crashing with a ObjCRuntime.RuntimeException.

Link to public reproduction project repository

https://github.com/billreiss/CollectionViewOverlappingIssue

Version with bug

9.0.70 SR7

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

iOS 18.5

Did you find any workaround?

Disable CollectionView V2 by commenting out or removing:

handlers.AddHandler<Microsoft.Maui.Controls.CollectionView,Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2>();

This reverts to the legacy CollectionViewHandler (V1), which handles header size changes correctly on iOS.

Relevant log output

ObjCRuntime.RuntimeException: Failed to marshal the Objective-C object 0x170efa840 (type: Microsoft_Maui_Platform_LayoutView). Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'Microsoft.Maui.Platform.LayoutView' does not have a constructor that takes one NativeHandle argument).
   at ObjCRuntime.Runtime.MissingCtor(IntPtr ptr, IntPtr klass, Type type, MissingCtorResolution resolution, IntPtr sel, RuntimeMethodHandle method_handle) in /Users/builder/azdo/_work/9/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:line 1412
   at ObjCRuntime.Runtime.ConstructNSObject[UIView](IntPtr ptr, Type type, MissingCtorResolution missingCtorResolution, IntPtr sel, RuntimeMethodHandle method_handle)
   at ObjCRuntime.Runtime.GetNSObject[UIView](IntPtr ptr, IntPtr sel, RuntimeMethodHandle method_handle, Boolean evenInFinalizerQueue)
   at ObjCRuntime.Runtime.GetNSObject[UIView](IntPtr ptr, IntPtr sel, RuntimeMethodHandle method_handle)
   at ObjCRuntime.Runtime.GetNSObject[UIView](IntPtr ptr)
   at ObjCRuntime.Runtime.GetNSObject[UIView](IntPtr ptr, Boolean owns)
   at UIKit.UIView.get_Superview() in /Users/builder/azdo/_work/9/s/xamarin-macios/src/build/dotnet/ios/generated-sources/UIKit/UIView.g.cs:line 4738
   at Microsoft.Maui.Platform.ViewExtensions.IsFinalMeasureHandledBySuperView(UIView view)
   at Microsoft.Maui.Platform.MauiView.LayoutSubviews()
   at Microsoft.Maui.Platform.MauiView.__Registrar_Callbacks__.callback_972_Microsoft_Maui_Platform_MauiView_LayoutSubviews(IntPtr pobj, IntPtr sel, IntPtr* exception_gchandle)

--------------------------------------------------------------------------------

$exception = {RuntimeException} ObjCRuntime.RuntimeException: Failed to marshal the Objective-C object 0x7fa0e2523120 (type: Microsoft_Maui_Platform_LayoutView). Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because th…
 Code = {int} 8027
 Data = {ListDictionaryInternal} Count = 0
 Error = {bool} true
 HResult = {int} -2146233088
 HasBeenThrown = {bool} true
 HelpLink = {string} null
 InnerException = {Exception} null
 Message = {string} "Failed to marshal the Objective-C object 0x7fa0e2523120 (type: Microsoft_Maui_Platform_LayoutView). Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'Microsoft.Maui.Platfor…"
 SerializationStackTraceString = {string} "   at ObjCRuntime.Runtime.MissingCtor(IntPtr ptr, IntPtr klass, Type type, MissingCtorResolution resolution, IntPtr sel, RuntimeMethodHandle method_handle) in /Users/builder/azdo/_work/9/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:line 1412\n   at ObjCRunt…"
 Source = {string} "Microsoft.iOS"
 StackTrace = {string} "   at ObjCRuntime.Runtime.MissingCtor(IntPtr ptr, IntPtr klass, Type type, MissingCtorResolution resolution, IntPtr sel, RuntimeMethodHandle method_handle) in /Users/builder/azdo/_work/9/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:line 1412\n   at ObjCRunt…"
 TargetSite = {RuntimeMethodInfo} Void MissingCtor(IntPtr, IntPtr, System.Type, MissingCtorResolution, IntPtr, System.RuntimeMethodHandle)
 _HResult = {int} -2146233088
 _data = {ListDictionaryInternal} Count = 0
 _dynamicMethods = {object[]} null
 _helpURL = {string} null
 _innerException = {Exception} null
 _message = {string} "Failed to marshal the Objective-C object 0x7fa0e2523120 (type: Microsoft_Maui_Platform_LayoutView). Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'Microsoft.Maui.Platfor…"
 _remoteStackTraceString = {string} null
 _source = {string} "Microsoft.iOS"
 _stackTraceString = {string} null
 _traceIPs = {IntPtr[]} IntPtr[39]
 _unused1 = {string} null
 _unused4 = {int} 0
 _unused6 = {object} null
 caught_in_unmanaged = {int} 0
 foreignExceptionsFrames = {MonoStackFrame[]} null
 native_trace_ips = {IntPtr[]} null
@vikher vikher added the t/bug Something isn't working label May 20, 2025
Copy link

We've found some similar issues:

If any of the above are duplicates, please consider closing this issue out and adding additional context in the original issue.

Note: You can give me feedback by 👍 or 👎 this comment.

@TamilarasanSF4853
Copy link
Contributor

This issue has been verified in Visual Studio Code 1.100.2 with .NET MAUI versions 9.0.50, 9.0.60, and 9.0.70 using the given reproduction sample. The issue does not reproduce when using CollectionViewHandler2. Additionally, we modified the sample to perform frequent layout changes, but were still unable to reproduce the issue.
Please find the sample and a reference video attached for your review.

Sample file - CollectionViewOverlappingIssue.zip

Screen.Recording.2025-05-21.at.4.28.09.PM.mov

@anandhan-rajagopal anandhan-rajagopal added platform/ios s/needs-info Issue needs more info from the author area-controls-collectionview CollectionView, CarouselView, IndicatorView s/triaged Issue has been reviewed s/no-repro Can no longer be reproduced on latest collectionview-cv2 labels May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView collectionview-cv2 platform/ios s/needs-info Issue needs more info from the author s/no-repro Can no longer be reproduced on latest s/triaged Issue has been reviewed t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants