简体   繁体   English

当我从 C# 调用 C++ 时间歇性崩溃

[英]Intermittent crash when I call C++ from C#

I have a C++ API which I cam calling from C# and it crashes intermittently after running normally sometimes up to 5h.我有一个 C++ API 我从 C# 调用它,它有时会在正常运行长达 5 小时后间歇性崩溃。

The C++ side: C++侧:

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:这是 C# 方面的内容:

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.这会在随机一段时间后随机崩溃,并且总是会出现一些关于损坏的 memory 的错误。

在此处输入图像描述

Or或者

在此处输入图像描述

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.我什至尝试在 while 循环开始时重置数组中先前使用的 Pakcages,但没有结果。 I am a C++ guy digging in C# here tbh.我是一个 C++ 人,在这里挖掘 C#。 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.注意:C++ 应用程序同时继续运行,没有任何错误或异常。

Maybe someone is spotting something very obvious I am missing here or any suggestion how to debug or diagnose this?也许有人发现了我在这里遗漏的非常明显的东西,或者有任何关于如何调试或诊断的建议?

Is using the 'i' variable in the 'j' loop in your C# code a deliberate choice?在 C# 代码的“j”循环中使用“i”变量是经过深思熟虑的选择吗?

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
    //------------^
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM