Skip to content
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

Strange behaviour after migrating MonoAndroid/Xamarin.iOS project to .net-android/.net-ios #9313

Open
DimaOrdenov opened this issue Sep 16, 2024 · 1 comment
Labels
needs-triage Issues that need to be assigned.

Comments

@DimaOrdenov
Copy link

DimaOrdenov commented Sep 16, 2024

Description

I am trying to migrate from Xamarin Native project to .NET (Android and iOS). I have a project MAIN that works fine with the following TargetFrameworks <TargetFrameworks>MonoAndroid12.0;Xamarin.iOS10;net5.0</TargetFrameworks>, but strange issue occurs after migrating to <TargetFrameworks>net7.0;net7.0-ios;net7.0-android</TargetFrameworks> (it will be .net8 or higher after successful migration to .NET at all).
Project MAIN is a wrapper for c++ libraries. To be more correct, there are two projects project MAIN depends on: project Android which references c++ library in *.so format; project iOS which references c++ library in binary format. I am not sure if iOS library format is *.a library format or something else as it provided by a partner as 'pure' binary without extension.
I have methods with DllImport attributes in the project MAIN. Also project MAIN contains structs, handlers and other used definitions for a C++ header file.
As far as I am running the app under Xamarin framework everything works fine. It's able to use all needed methods.
But when I change the framework to .NET some methods break.
To be more precise, I'll show methods which work and which are not.
This is all Utf8String methods and structs I am using:

C++

Rtc360_Utf8StringHandle Rtc360_Utf8String_New(const char* value);

void Rtc360_Utf8String_Release(Rtc360_Utf8StringHandle instance);

const char* Rtc360_Utf8String_Data(Rtc360_Utf8StringHandle instance);

typedef struct
{
    Rtc360_Handle handle;
} Rtc360_Utf8StringHandle;

typedef uint64_t Rtc360_Handle;

C#

[DllImport(InternalConstants.DLL_NAME, EntryPoint = "Rtc360_Utf8String_New")]
internal static extern Rtc360_Utf8StringHandle Rtc360_Utf8String_New([In] [MarshalAs(UnmanagedType.LPStr)] string value);

[DllImport(InternalConstants.DLL_NAME, EntryPoint = "Rtc360_Utf8String_Release")]
internal static extern void Rtc360_Utf8String_Release(Rtc360_Utf8StringHandle instance);

[DllImport(InternalConstants.DLL_NAME, EntryPoint = "Rtc360_Utf8String_Data")]
internal static extern IntPtr Rtc360_Utf8String_Data(Rtc360_Utf8StringHandle instance);

[StructLayout(LayoutKind.Sequential)]
internal struct Rtc360_Utf8StringHandle
{
    internal ulong handle;
}

Utf8String successfully works for the following method and struct

C++

void Rtc360_MeasurementWorkflow_Start(Rtc360_MeasurementWorkflowHandle instance, Rtc360_MeasurementParameters measurementParameters);

typedef struct
{
    ... other fields ...
    Rtc360_Utf8StringHandle setupName;
} Rtc360_MeasurementParameters;

C#

[DllImport(InternalConstants.DLL_NAME, EntryPoint = "Rtc360_MeasurementWorkflow_Start")]
internal static extern void Rtc360_MeasurementWorkflow_Start(Rtc360_MeasurementWorkflowHandle instance, Rtc360_MeasurementParameters measurementParameters);

[StructLayout(LayoutKind.Sequential)]
internal struct Rtc360_MeasurementParameters
{
    ... other fields ...
    internal Rtc360_Utf8StringHandle setupName;
}

But this method breaks after changing Xamarin to .NET

C++

void Rtc360_DataManipulationWorkflow_SetJobName(Rtc360_DataManipulationWorkflowHandle instance, Rtc360_Uuid jobId, Rtc360_Utf8StringHandle name);

C#

[DllImport(InternalConstants.DLL_NAME, EntryPoint = "Rtc360_DataManipulationWorkflow_SetJobName")]
internal static extern void Rtc360_DataManipulationWorkflow_SetJobName(Rtc360_DataManipulationWorkflowHandle instance, Rtc360_Uuid jobId, Rtc360_Utf8StringHandle name);

By 'breaks' I mean that the library throws an error that created Rtc360_DataManipulationWorkflow_SetJobName passed by argument name do not persist in the library. However, the library logs the created handle (using Rtc360_Utf8String_New) during the creation process and passed argument Rtc360_Utf8StringHandle name in the error message. They are different.
I've made a call with the partner and he said that there is no special logic that could change the string handle inside of Rtc360_DataManipulationWorkflow_SetJobName.
Also, as I've said, it works for Xamarin.

Where I can search the answers? Maybe I can try to 'play' with marshalling?
I've tried to use LibraryImport attribute but no success.
Maybe there were changes in marshalling? Should I take a look at other projects in my solution?

Feel free to request more info so I could explain the details better.

Steps to Reproduce

Steps to reproduce probably will be 'unprovidable' as I have internal SDK (binary libraries files) and the app. But I am open for any further communications. We also can make a private debug session if needed.

Link to public reproduction project repository

No response

Version with bug

.NET 7

Is this a regression from previous behavior?

Yes, this used to work in Xamarin Native

Last version that worked well

Xamarin latest

Affected platforms

iOS, Android

Affected platform versions

Doesn't matter

Did you find any workaround?

No workaround

Relevant log output

No response

Copy link

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!

Closed similar issues:

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

@PureWeen PureWeen transferred this issue from dotnet/maui Sep 19, 2024
@dotnet-policy-service dotnet-policy-service bot added the needs-triage Issues that need to be assigned. label Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-triage Issues that need to be assigned.
Projects
None yet
Development

No branches or pull requests

1 participant