在一个用gdi+绘图程序中出现异常,程序中使用了不安全代码为BitmapData手动赋值(之所以如此主要是因为c#中setpixs方法实在太慢,大量描点会使系统没有响应)。另外该问题在有的机器上不出现(我共用了三台机器测试,其中两台没有问题,一台ibm笔记本会出现异常),出现问题时会把绘制bitmap的panel中打个大红叉。
   由于问题较为复杂,能解决问题我愿高分相谢!
在 System.AccessViolationException 中第一次偶然出现的“radar_overprinting.exe”类型的异常
“radar_overprinting.vshost.exe”(托管): 已加载“C:\WINDOWS\assembly\GAC_MSIL\System.Transactions.resources\2.0.0.0_zh-CHS_b77a5c561934e089\System.Transactions.resources.dll”
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>未处理的异常</Description><AppDomain>radar_overprinting.vshost.exe</AppDomain><Exception><ExceptionType>System.AccessViolationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>尝试读取或写入受保护的内存。这通常指示其他内存已损坏。</Message><StackTrace>   在 radar_overprinting.mdichild.panel1_Paint(Object sender, PaintEventArgs e) 位置 D:\project\radar_overprinting\radar_overprinting\mdichild.cs:行号 319
   在 System.Windows.Forms.Control.OnPaint(PaintEventArgs e)
   在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
   在 System.Windows.Forms.Control.WmPaint(Message&amp;amp; m)
   在 System.Windows.Forms.Control.WndProc(Message&amp;amp; m)
   在 System.Windows.Forms.ScrollableControl.WndProc(Message&amp;amp; m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp;amp; m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp;amp; m)
   在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp;amp; msg)
   在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   在 System.Windows.Forms.Application.Run(Form mainForm)
   在 radar_overprinting.Program.Main() 位置 D:\project\radar_overprinting\radar_overprinting\Program.cs:行号 17
   在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   在 System.Threading.ThreadHelper.ThreadStart()</StackTrace><ExceptionString>System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
   在 radar_overprinting.mdichild.panel1_Paint(Object sender, PaintEventArgs e) 位置 D:\project\radar_overprinting\radar_overprinting\mdichild.cs:行号 319
   在 System.Windows.Forms.Control.OnPaint(PaintEventArgs e)
   在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
   在 System.Windows.Forms.Control.WmPaint(Message&amp;amp; m)
   在 System.Windows.Forms.Control.WndProc(Message&amp;amp; m)
   在 System.Windows.Forms.ScrollableControl.WndProc(Message&amp;amp; m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp;amp; m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp;amp; m)
   在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp;amp; msg)
   在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   在 System.Windows.Forms.Application.Run(Form mainForm)
   在 radar_overprinting.Program.Main() 位置 D:\project\radar_overprinting\radar_overprinting\Program.cs:行号 17
   在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   在 System.Threading.ThreadHelper.ThreadStart()</ExceptionString></Exception></TraceRecord>

解决方案 »

  1.   

    你程序需要的内存空间太大,IBM笔记本的内存不够.
      

  2.   

    radar_overprinting.mdichild.panel1_Paint(Object sender, PaintEventArgs e) 位置 D:\project\radar_overprinting\radar_overprinting\mdichild.cs:行号 319 这里你写的代码是什么??
    贴出来
      

  3.   

    1. 确认你申请的内存是足够的, 如果没有申请足够内存, 那自然会access violation.2. 对于与非托管代码交互时, 如果有内存传递, 需要将内存pin住, 也就是fixed关键字. 
       不然, 如果发生了GC, GC可能会将你的内存移动, 造成access violation.
      

  4.   

    你的IBM笔记本太老了,请尝试更换带有的Lenovo标志的Thinkpad笔记本。XD
      

  5.   

    1. 确认你申请的内存是足够的, 如果没有申请足够内存, 那自然会access violation. 2. 对于与非托管代码交互时, 如果有内存传递, 需要将内存pin住, 也就是fixed关键字. 
      不然, 如果发生了GC, GC可能会将你的内存移动, 造成access violation.