I'm trying to run an application on a docker image on an Arm board.
The application is written in C# but it uses a C++ library (I have sources for both). I'm able to use it normally on Windows and on Linux x64.
I was able to compile the C++ code for ARM64 (at least I think so):

I'm having issues getting the result string back from the C++ library.
The library has a single exported function that take a string as input and return another one as output.
Both the strings are XML and I can see them correctly using the C++ output with cout << s. I can see it from the docker logs of the container.
This is the [dllimport] I have so far, I believe that this is the source of my issues:
[DllImport("BoxOptEngine_arm64.so", EntryPoint = "Optimize", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private static extern nint Optimize_Arm64(string xmlEncodedOptimization);
And this is the export on the C++ side:
extern "C" {
const char* Optimize(const char* xmlEncodedInstance) {
...
return s.c_str();
}
}
The funny thing is that sometimes I get a correct output but most times not. And I get something like this but different every time (the highlighted characters before the comma):

[edit] This is how I call the function from my C# code:
nint resultPtr = Optimize_Arm64(serializedRequest);
xml = Marshal.PtrToStringAnsi(resultPtr);
Do you have any clue? Thanks in advance!
s.c_str();what issand where it is allocated?return s.c_str();is a death sentence.std::stringonly guarantees thatc_str()is valid as long assis alive. Since you're returning from a function, you did not uphold your end of the bargain.