Intermittent crash when I call C++ from C#

Viewed 90

I have a C++ API which I cam calling from C# and it crashes intermittently after running normally sometimes up to 5h.

The C++ side:

struct Doubles
{
    double d1
    double d2;
};

struct Package
{
    char name[20]{};
    int lenD1{};
    Doubles* d1;
    int lenD2{};
    Doubles* d2{};
    int64_t duration;
};

// Basically I am passing an array of Packages preallocated on C# side and let C++ to fill in
extern "C" __declspec(dllimport) int api(int packageLen, Package* pkg);

Here is what is on C# side:

struct Doubles
{
    public double d1;
    public double d2;
};

unsafe struct Package
{
    public fixed byte[20]; // here we use byte as char on C# is 2 bytes unlike C++ 
    public int lenD1;
    public unsafe Doubles* d1;
    public int lenD2;
    public unsafe Doubles* d2;
    long duration;
};

[DllImport("..path/api.dll")]
[return: MarshalAs(UnmanagesType.I4)]
[SuppressUnmanagedCodeSecurity]
public static extern int api([MarshalAs(UnmanagedType.LPStr)] int len, Package* packages);

unsafe static void Main(string[] args)
{
    int len = 10;
    fixed(Package* pkgs = new Package[len]) // create the array of packages
    {
        for(int i=0; i<len;++i) // and allocate space for individual one for C++ to fill in the data
        {
            fixed(Doubles* d_1 = new Double[20])
            {
                fixed(Doubles* d_2 = new Double[20])
                {
                    pkgs[i].d1 = d_1;
                    pkgs[i].d2 = d_2;
                    pkgs[i].lenD1 = 20;
                    pkgs[i].lenD2 = 20;
                } 
            } 
        }

        while(true)
        {
            int res = api(len,pkgs); // res is number of pkgs filled in
            
            for(int k=0; k<res;++k)
            {
                string s = "";
                for(int j=0; j<20;++j)
                {
                    if(pkgs[i].name[k] == '\n'){break;}
                    s+=(char)pkgs[i].name[k]; // converting filled byte[] to string
                }
            }
        }
    }

And this randomly crashes after random period of time and always some error regarding corrupt memory.

enter image description here

Or

enter image description here

For instance something like aboves. I even tried to reset the previously used Pakcages in the array in the beginning of while loop but no results. I am a C++ guy digging in C# here tbh. There is no stack trace, line number no nothing, just crashing randomly after working for hours sometimes.

Note: C++ app meanwhile continues to run without any errors or exceptions.

Maybe someone is spotting something very obvious I am missing here or any suggestion how to debug or diagnose this?

1 Answers

Is using the 'i' variable in the 'j' loop in your C# code a deliberate choice?

for(int j=0; j<20;++j)
{
    if(pkgs[i].name[k] == '\n'){break;}
    //------^
    s+=(char)pkgs[i].name[k]; // converting filled byte[] to string
    //------------^
}
Related