随机访问非托管DLL中的违规

时间:2015-12-13 22:29:37

标签: random pinvoke marshalling access-violation

在本机DLL中,我有:

ReturnMode Decoder::Decode(int16 samples[], int num_samples, char** hypothesis){

char *hypstr = NULL;

try
{
        // some other...

        if (s3_decode(&decoder, NULL, &hypstr, NULL) == 0)
        {

            *hypothesis = (char*)CoTaskMemAlloc(strlen(hypstr) + 1);
            strcpy(*hypothesis, hypstr);
        }


        if (hypstr != NULL)
        {
            free(hypstr);
            hypstr = NULL;

        }

}
catch (...)
{
    return FatalError;
}


return Ok;}

编辑:用C#打电话:

            Task.Factory.StartNew(() => {

            // some other 

            while (!Stopflag)
            {                   
               IntPtr Hypothesis;
                if ((Result = DecoderWrap.Decode(mDecoder, samples, num_samples, out Hypothesis)) == ShProxy.ReturnMode.Ok)
                {
                    var hypothesis = Marshal.PtrToStringAnsi(Hypothesis);
                    if (!String.IsNullOrEmpty(hypothesis))
                    {                            
                        hypothesis = hypothesis.Trim();
                        ConvertAsciiToUnicode(ref hypothesis);
                        // this is an event 
                        TextReceived(hypothesis, 0);
                    }
                }
                else
                {
                    logger.Fatal("Cannot do decode");
                    break;
                }
            }
            });

以这种方式导入:

    [DllImport("ShenavaDec.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
    static public extern ShProxy.ReturnMode Decode(IntPtr pDec, short[] samples, int num_samples, out IntPtr Hypothesis);

经常解码方法调用但AV随机发生在本机DLL中的s3_decode API中。 现在我想知道我的错误在哪里? 它与编组数据有关还是没有。 谢谢。

0 个答案:

没有答案