dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.83k stars 1.67k forks source link

'Microsoft.Maui.Platform.ContentView' does not have a constructor that takes one NativeHandle argument #23139

Open softlion opened 2 weeks ago

softlion commented 2 weeks ago

Description

I see that error in Sentry.

Failed to marshal the Objective-C object 0x143c0ffe0 (type: Microsoft_Maui_Platform_ContentView). 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.ContentView' does not have a constructor that takes one NativeHandle argument).

The stack trace is:

id Runtime.MissingCtor(IntPtr, IntPtr, Type, MissingCtorResolution, IntPtr, RuntimeMethodHandle)
In App

UIView Runtime.ConstructNSObject<UIView>(IntPtr, Type, MissingCtorResolution, IntPtr, RuntimeMethodHandle)
In App

UIView Runtime.GetNSObject<UIView>(IntPtr, IntPtr, RuntimeMethodHandle, bool) x 3
In App

Called from: UIView UIView.get_Superview()

Show 17 more frames

void MaterialTextField.AnimateToInactiveOrFocusedState(bool animate)

Where MaterialTextField is a custom control, and AnimateToInactiveOrFocusedState creates an Animation object to animate some properties of existing UI objects. Nothing special.

Steps to Reproduce

Not sure. But it looks like it is related to The RealParent on Microsoft.Maui.Controls.Button has been Garbage Collected. This should never happen. Please log a bug: https://github.com/dotnet/maui

Link to public reproduction project repository

No response

Version with bug

8.0.60 SR6

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.40 SR5

Affected platforms

iOS

Affected platform versions

All

Did you find any workaround?

no

Relevant log output

No response

github-actions[bot] commented 2 weeks ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

drasticactions commented 2 weeks ago

Could you create a reproduction sample showing how you got into this condition?

Assuming you don't see that issue with other, non-custom, text fields, it may be the act of creating custom text fields in general (or something specific with the ones you wrote) could be causing this to happen, but without that context or a way to reproduce it it will be difficult to isolate your issue.

softlion commented 1 week ago

I have a custom control called MaterialTextField whose XAML is a ContentView.
It does not have any custom handler or renderer.
Nothing fancy.

image

If a constructor from a Handle is missing in iOS, for ContentView for example, then this error will be triggered. And that's not from my code, but the maui iOS code related to the control used for ContentView.

Have you correctly created all required constructors on ContentView, and are they correctly kept in release mode from LLVM optimization ?

PureWeen commented 1 week ago

Usually this happens because the Managed component has been garbage collected but the unmanaged side hasn't.

This can often times happen with things like Animations, where they are going to resolve on the platform side while the managed side things all references to said class are no longer valid.

So, my guess here is that the animation is being applied to a MaterialTextField that isn't pinned on the managed side of things at all so it gets garbage collected. Something resolves on the native side which then triggers the manage code to try and recreate said thing.

We could add these constructors but that's almost always just fixing the symptom not the actual problem

BenDTU commented 1 day ago

I'm seeing the issue as well, but in my case it's being triggered by changing a property which is attached to an image button's IsVisible property, which is set on navigate to a transient page. Removing the binding fixes the issue. It does not trigger with any sort of consistency - some builds it never happens, some it happens on the second or third navigate.