通信人家园

 找回密码
 注册

只需一步,快速开始

短信验证,便捷登录

搜索

军衔等级:

  下士

注册:2014-1-16
跳转到指定楼层
1#
发表于 2015-5-31 16:34:28 |只看该作者 |正序浏览
本帖最后由 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);处报的错,可是我不明白为什么会在此报错,而且为什么程序刚开始能够正常运行,运行一段时间后会出错???

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

举报本楼

本帖有 2 个回帖,您需要登录后才能浏览 登录 | 注册
您需要登录后才可以回帖 登录 | 注册 |

版规|手机版|C114 ( 沪ICP备12002291号-1 )|联系我们 |网站地图  

GMT+8, 2025-7-24 01:48 , Processed in 0.326947 second(s), 20 queries , Gzip On.

Copyright © 1999-2025 C114 All Rights Reserved

Discuz Licensed

回顶部