标题: C#视频通信程序运行一段时间后报错:AccessViolationException was unhandled by user[查看完整版帖子][打印本页]
时间: 2015-5-31 16:34
作者: longyuchen 标题: C#视频通信程序运行一段时间后报错:AccessViolationException was unhandled by user
本帖最后由 longyuchen 于 2015-5-31 16:38 编辑
C#视频通信程序运行一段时间后报错:AccessViolationException was unhandled by user code:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
程序代码如下:
MemoryStream ms = new MemoryStream();
Bitmap b;
/// <summary>
/// 捕获事件的触发及回调
/// </summary>
private void OnCaptureDone()
{
lock (locker)
{
Trace.WriteLine("!!DLG: OnCaptureDone");
try
{
int hr;
if (sampGrabber == null)
return;
hr = sampGrabber.SetCallback(null, 0);
int w = videoInfoHeader.BmiHeader.Width;
int h = videoInfoHeader.BmiHeader.Height;
if (((w & 0x03) != 0) || (w < 32) || (w > 4096) || (h < 32) || (h > 4096))
return;
int stride = w * 3;
GCHandle handle = GCHandle.Alloc(savedArray, GCHandleType.Pinned);
IntPtr scan0 = handle.AddrOfPinnedObject();
scan0 += (h - 1) * stride;
b = new Bitmap(w, h, -stride, PixelFormat.Format24bppRgb, (IntPtr)scan0);
savedArray = null;
Image old = m_capImg;
m_capImg = b;
//发送视频消息
ms = new MemoryStream();
b.Save(ms, ImageFormat.Jpeg);
byte[] byVideoMsg = ms.ToArray();
b = null;
//ms = null;
DataSend(byVideoMsg, Messages.MsgType.Video);
handle.Free();
byVideoMsg = null;
if (old != null)
old.Dispose();