Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Kinect:AccessViolationException_C#_.net_Exception_Kinect_Access Violation - Fatal编程技术网

C# Kinect:AccessViolationException

C# Kinect:AccessViolationException,c#,.net,exception,kinect,access-violation,C#,.net,Exception,Kinect,Access Violation,我们使用Kinect来流式传输用户的可自定义视频,您可以在不停止流的情况下选择流类型(颜色、深度,两者都有显示跟踪骨架的选项) 如果我们更改流类型,我们的程序运行良好,但在启用骨架的几帧(仅在某些PC上)后,应用程序崩溃,显示: Unhandled Exception: System.AccessViolationException. 这是我们的代码: private void KinectAllFramesReady(object sender, AllFramesReadyEventArg

我们使用Kinect来流式传输用户的可自定义视频,您可以在不停止流的情况下选择流类型(颜色、深度,两者都有显示跟踪骨架的选项)

如果我们更改流类型,我们的程序运行良好,但在启用骨架的几帧(仅在某些PC上)后,应用程序崩溃,显示:

Unhandled Exception: System.AccessViolationException.
这是我们的代码:

private void KinectAllFramesReady(object sender, AllFramesReadyEventArgs e)
{
    using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())        
    {
        currentSkeleton = GetSkeletonFromSkeletonFrame(skeletonFrame);

        if (this.frameMode == Constants.VIDEO_DEPTH_MODE)
            currentFrame = GetByteArrayFromDepthFrame(e);
        else if (this.frameMode == Constants.VIDEO_COLOR_MODE)
            currentFrame = GetByteArrayFromColorFrame(e);
        else if (this.frameMode == Constants.VIDEO_NONE_MODE)
            currentFrame = GetByteFromBlankFrame();


        if (isSkeleton)
        {
            currentFrame = OverlapSkeleton(currentFrame, this.currentSkeleton);
        }
    }
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private byte[] OverlapSkeleton(byte[] currentFrame, Skeleton S)
{
    Bitmap tempBitmap = new Bitmap(Constants.KINECT_DEFAULT_CAPTURE_WIDTH, Constants.KINECT_DEFAULT_CAPTURE_HEIGHT, PixelFormat.Format32bppArgb);
    BitmapData tempBmapData = tempBitmap.LockBits(new Rectangle(0, 0, Constants.KINECT_DEFAULT_CAPTURE_WIDTH, Constants.KINECT_DEFAULT_CAPTURE_HEIGHT), ImageLockMode.ReadWrite, tempBitmap.PixelFormat);

    IntPtr ptr = tempBmapData.Scan0;

    for (int i = 0, offset = 0; i < Constants.KINECT_DEFAULT_CAPTURE_HEIGHT; i++)
    {
        Marshal.Copy(currentFrame, offset, ptr, Constants.KINECT_DEFAULT_CAPTURE_WIDTH << 2);
        offset += Constants.KINECT_DEFAULT_CAPTURE_WIDTH << 2;
        ptr += tempBmapData.Stride;
    }

    tempBitmap.UnlockBits(tempBmapData);
    Graphics g = Graphics.FromImage(tempBitmap);

    //  Upper Body
    DrawBone(JointType.Head, JointType.ShoulderCenter, S, g);
    DrawBone(JointType.ShoulderCenter, JointType.Spine, S, g);
    DrawBone(JointType.Spine, JointType.HipCenter, S, g);
    //  Left Arm
    DrawBone(JointType.ShoulderCenter, JointType.ShoulderLeft, S, g);
    DrawBone(JointType.ShoulderLeft, JointType.ElbowLeft, S, g);
    DrawBone(JointType.ElbowLeft, JointType.WristLeft, S, g);
    DrawBone(JointType.WristLeft, JointType.HandLeft, S, g);
    //  Right Arm
    DrawBone(JointType.ShoulderCenter, JointType.ShoulderRight, S, g);
    DrawBone(JointType.ShoulderRight, JointType.ElbowRight, S, g);
    DrawBone(JointType.ElbowRight, JointType.WristRight, S, g);
    DrawBone(JointType.WristRight, JointType.HandRight, S, g);
    //  Left leg
    DrawBone(JointType.HipCenter, JointType.HipLeft, S, g);
    DrawBone(JointType.HipLeft, JointType.KneeLeft, S, g);
    DrawBone(JointType.KneeLeft, JointType.AnkleLeft, S, g);
    DrawBone(JointType.AnkleLeft, JointType.FootLeft, S, g);
    //  Right Leg
    DrawBone(JointType.HipCenter, JointType.HipRight, S, g);
    DrawBone(JointType.HipRight, JointType.KneeRight, S, g);
    DrawBone(JointType.KneeRight, JointType.AnkleRight, S, g);
    DrawBone(JointType.AnkleRight, JointType.FootRight, S, g);

    byte[] bytes = new byte[Constants.KINECT_COLOR_FRAME_SIZE];
    Marshal.Copy(tempBmapData.Scan0, bytes, 0, Constants.KINECT_COLOR_FRAME_SIZE);

    return bytes;
}

Constants.KINECT_COLOR_FRAME_SIZE = 1228800;
Constants.KINECT_DEFAULT_CAPTURE_WIDTH = 640;
Constants.KINECT_DEFAULT_CAPTURE_HEIGHT = 480;
private void KinectAllFramesReady(对象发送方,AllFramesReadyEventArgs e)
{
使用(SkeletonFrame SkeletonFrame=e.OpenSkeletonFrame())
{
currentSkeleton=GetSkeletonFromSkeletonFrame(skeletonFrame);
if(this.frameMode==Constants.VIDEO\u DEPTH\u MODE)
currentFrame=GetByteArrayFromDepthFrame(e);
else if(this.frameMode==Constants.VIDEO\u COLOR\u MODE)
currentFrame=GetByteArrayFromColorFrame(e);
else if(this.frameMode==Constants.VIDEO\u NONE\u模式)
currentFrame=GetByteFromBlankFrame();
伊夫(伊斯凯尔顿)
{
currentFrame=重叠骨架(currentFrame,this.currentSkeleton);
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInline)]
私有字节[]重叠骨架(字节[]当前帧,骨架S)
{
位图tempBitmap=新位图(Constants.KINECT\u DEFAULT\u CAPTURE\u WIDTH、Constants.KINECT\u DEFAULT\u CAPTURE\u HEIGHT、PixelFormat.Format32bppArgb);
BitmapData tempBmapData=tempBitmap.LockBits(新矩形(0,0,常数.KINECT_默认值_捕获_宽度,常数.KINECT_默认值_捕获_高度),ImageLockMode.ReadWrite,tempBitmap.PixelFormat);
IntPtr ptr=tempBmapData.Scan0;
对于(int i=0,offset=0;iMarshal.Copy(currentFrame、offset、ptr、Constants.KINECT\u DEFAULT\u CAPTURE\u WIDTH这不是一个真正的解决方案,但更像是一种黑客/变通方法

无论如何,我发现异常是随机抛出的,但并不频繁,因此解决方法是用

[HandleProcessCorruptedStateExceptions]

执行此操作时,可以使用try/catch捕获CSE。生成的代码是:

[MethodImpl(MethodImplOptions.AggressiveInlining)]
[HandleProcessCorruptedStateExceptions]
[SecurityCritical]
private byte[] OverlapSkeleton(byte[] currentFrame, Skeleton S)
{
    if (S == null)
        return currentFrame;

    try
    {
        //The above code with Marshall.copy and lines drawing
    }
    catch(Exception)
    {
        return currentFrame;
    }

同样,它不能解决问题,但由于没有性能影响,而且我们也没有想法,这是我必须提供的最佳解决方案。如果有人有更好的/可行的想法,我们欢迎。

当程序崩溃时(Marshal.Copy),currentFrame、offset和ptr的内容是什么?还有一个问题:您使用AggressiveInline的原因是什么?offset的最后一个值是1228800,ptr是536002560。当i(帧行)时引发异常is 479我们使用Affessive内联只是为了获得一点性能提升,但我们已经尝试删除它,并且没有任何更改currentFrame充满了表示当前帧字节的值(我与Breatse0属于同一团队),当您的程序崩溃时,有多大(长度方面)您的当前帧是否一致?大小是否一致?它与tempBmapData.Stride x tempBitmap.Height的值有何关系?tempBmapData.Stride是2560,tempBitmap.Height是480,产品实际上是1228800。令人惊讶的是添加了一个控制台。writeline()在for循环中为每次迭代打印这些值会减慢计算速度,但这次应用程序不会崩溃。您认为这与某种方式有关吗?
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[HandleProcessCorruptedStateExceptions]
[SecurityCritical]
private byte[] OverlapSkeleton(byte[] currentFrame, Skeleton S)
{
    if (S == null)
        return currentFrame;

    try
    {
        //The above code with Marshall.copy and lines drawing
    }
    catch(Exception)
    {
        return currentFrame;
    }