我们正在使用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;
Marshal.Copy()行抛出异常。 我们已经在SO上看到有人建议不要一次复制整个数据块,而是循环复制图像的每一行(参见OverlapSkeleton中的for循环),但它不起作用。
奇怪的是,在我们开发的机器上(i5-2410m@2.30GHz/4Gb RAM和i5-m560@2.67GHz/4Gb RAM)它们都运行良好,但是在将托管应用程序的机器上(i3-2377m) @ 1.5GHz / 4Gb RAM)它在骨架启用后2-3秒崩溃,抛出异常。
操作系统是Win7 SP1,适用于每台计算机的.NET Framework 4.5。
关于什么会引起这种看似随机的异常的想法?
答案 0 :(得分:1)
这不是一个真正的解决方案,而是更多的黑客/工作方式。
无论如何,我发现异常是随机抛出的,但不常见,因此解决方法是使用
装饰OverlapSkeleton方法[HandleProcessCorruptedStateExceptions]
和
[SecurityCritical]
这样做可以使用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;
}
同样,它没有解决问题,但由于没有性能影响,我们也没有想法,这是我必须提供的最佳解决方案。如果有人有更好/工作的想法,欢迎。