0

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): enter image description here

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): enter image description here

[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!

5
  • 1
    Side note: Images of test are looked upon very unfavorably (For many good reasons) The first image should be replaced with text. The second one should stay, but be supplemented with a cut-n-paste of the text for those who cannot view the image. Commented Mar 13 at 20:06
  • 2
    s.c_str(); what is s and where it is allocated? Commented Mar 13 at 20:07
  • 2
    return s.c_str(); is a death sentence. std::string only guarantees that c_str() is valid as long as s is alive. Since you're returning from a function, you did not uphold your end of the bargain. Commented Mar 13 at 20:07
  • What are the results of running your code on a desktop platform? Commented Mar 13 at 20:10
  • I changed my code to use a string builder. I guess, as y'all said as well, that I have to allocate the space for the string in C# Now it works Commented Mar 13 at 20:22

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.