xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.48k stars 518 forks source link

iOS binding library - Crash when app launches #21546

Open jmpdi opened 3 weeks ago

jmpdi commented 3 weeks ago

Apple platform

iOS

Framework version

net8.0-*

Affected platform version

JetBrains Rider 2024.2.7, .NET workload ios 18.0.8303/8.0.100, Xcode 16.0, iPhone 11 Pro iOS 17.7

Description

I'm trying to bind Braintree SDK for iOS v6.23.3.

The binding and app build successfully, but the app crashes at the launch screen. I'm testing on a physical device (iPhone 11 Pro, iOS 17.7). I haven't tried it in the simulator.

This repo demonstrates the problem. Build logs attached. Console log of the crash attached.

The crash is related to particular Braintree APIs. When I exclude the problematic APIs, the app launches successfully. I've added four "#define STARTUP_CRASH..." statements to the top of ApiDefinitions to isolate the problematic APIs. If I comment out all four of those "#define" statements, the app launches successfully. But if one or more are uncommented, the app will crash at launch.

The problematic APIs all relate to the BTPaymentMethodNonce type within BraintreeCore.xcframework. They either inherit from that type or utilize a descendent of that type.

Also, I see this warning in the build:

warning MT7091: The framework PPRiskMagnes.framework is a framework of static libraries, and will not be copied to the app.

If I change the NativeReference for that framework to "Static", I get a linker error during the build.

Any advice/insight is appreciated. Thanks.

Steps to Reproduce

  1. Download this repo.
  2. Build solution.
  3. Install Sample app on iOS device.
  4. Observe Sample app crash at launch screen.

Did you find any workaround?

No workaround.

Build logs

MSBuild_Log_Startup_Crash_241029.zip

Console_Log_Startup_Crash_241029.txt

rolfbjarne commented 3 weeks ago

This is interesting, I can reproduce the crash.

It happens very early on, when we load the Objective-C types we know about, in fact it's our own generated Objective-C code that crashes, something equivalent

handle = [_TtC13BraintreeCore20BTPaymentMethodNonce class];

Would you happen to know if there's an Xcode sample project somewhere that exercises the Objective-C version of the API you want to bind?

jmpdi commented 3 weeks ago

@rolfbjarne - Thanks for investigating this!

I searched for a sample project that uses the Objective-C APIs, but I didn't find anything.

I also asked the Braintree team about it. They don't have an Objective-C sample, but do say the SDK is compatible with Objective-C. See https://github.com/braintree/braintree_ios/issues/1450#issuecomment-2450669862

Are there any other options to isolate/debug the problem?

Thanks again!

rolfbjarne commented 2 weeks ago

I'll have a look next week and see what I can figure out.

rolfbjarne commented 2 weeks ago

I've figured out the problem, and a fix is in progress.

In the meantime, you can add this to your csproj as a workaround:

<PropertyGroup>
    <Registrar>dynamic</Registrar>
</PropertyGroup>
rolfbjarne commented 2 weeks ago

The problem is that the classes causing problems are stub classes, and we don't handle stub classes correctly (we've never run into them before):

jmpdi commented 1 week ago

@rolfbjarne -- Thank you! With your workaround, the startup crash is gone and I can access the Braintree APIs from my iOS app. I appreciate your help with this and I'm looking forward to the full fix. Thanks again!