在一个用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; m)
在 System.Windows.Forms.Control.WndProc(Message&amp; m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&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; m)
在 System.Windows.Forms.Control.WndProc(Message&amp; m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&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>
由于问题较为复杂,能解决问题我愿高分相谢!
在 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; m)
在 System.Windows.Forms.Control.WndProc(Message&amp; m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&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; m)
在 System.Windows.Forms.Control.WndProc(Message&amp; m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&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>
贴出来
不然, 如果发生了GC, GC可能会将你的内存移动, 造成access violation.
不然, 如果发生了GC, GC可能会将你的内存移动, 造成access violation.