通信人家园

标题: 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();
      
                }
                catch (Exception e)
                {
                    //MessageBox.Show(ee.Message.ToString());
                    MessageBox.Show(this, "不能捕获图片!\r\n" + e.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            
        }
跟踪报错,通过观察报错详细,具体为:
无标题.png
无标题2.png
也就是说是在图片保存时b.Save(ms, ImageFormat.Jpeg);处报的错,可是我不明白为什么会在此报错,而且为什么程序刚开始能够正常运行,运行一段时间后会出错???

恳请大牛指点!!!谢谢!!!



附件: 无标题.png (2015-5-31 16:30, 68.65 KB) / 下载次数 0
https://www.txrjy.com/forum.php?mod=attachment&aid=MjYxMjE0fDNkZTc5Njc5fDE3NTMxMTM2NTJ8MHww

附件: 无标题2.png (2015-5-31 16:32, 24.48 KB) / 下载次数 0
https://www.txrjy.com/forum.php?mod=attachment&aid=MjYxMjE1fDNlYWVlMzk4fDE3NTMxMTM2NTJ8MHww
时间:  2015-6-4 19:54
作者: longyuchen

有没有人懂啊???
有没有人遇到过类似的错误,当时是怎么解决的???
时间:  2015-6-6 11:16
作者: longyuchen

居然没人回复。。。




通信人家园 (https://www.txrjy.com/) Powered by C114