我正在为C#实现C ++ Dll。但是当我运行我的程序时,会发生AccessViolationException。这是代码。 在C ++中:
extern "C" DLLIMPORT double __cdecl ErrorCalculate(double* pBufferA, double* pBufferB,__int32 length)
{
__m128d xfdLoadA;
__m128d xfdLoadB;
const double* pA = pBufferA;
const double* pB = pBufferB;
//do somthing
for(int i=0;i<length/2;i++)
{
xfdLoadA = _mm_load_pd(pA);//error occur at this line
xfdLoadB = _mm_load_pd(pB);
pA+=2;
pB+=2;
// do somthing
}
// do somthing
}
在C#中:
[DllImport("test.dll", EntryPoint = "ErrorCalculate", CallingConvention = CallingConvention.Cdecl)]
static extern double ErrorCalculate(double[] pBufferA, double[] pBufferB, int length);
public void frameProcessing(Image<Gray, byte> frame){
//do something
SSE4 sse4 = new SSE4();
unsafe
{
//length of samplePoint is the same as tempPoint
fixed (double* sample = samplePoint)
{
fixed (double* test = tempPoint)
{
errorSum = sse4.ErrorCalc(sample, test, length);
}
}
}
if(errorSum<=threshold)
{//do something}
}
frameProcessing将被多次调用。有时错误将在我第一次调用frameProcessing时发生,有时是第二次。我发现错误发生在_mm_load_pd(pA)。我确定pA和pB不为空(可以打印出两个数组的值)。任何帮助将不胜感激。