本文介绍了这是什么意思时,由于System.AccessViolation异常.NET应用程序崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序本身是长2000行,以便将没有意义粘贴在这里code,特别是因为异常的一个用户收到的不给任何提示,以什么我$ C $的一部分C是造成问题的原因。

该应用程序,顺便说一句,只是一个Windows窗体有一个DataGridView通常会显示不超过几百行的数据,以及其他一些控件的更多。坠毁前,这是载入DataGridView中的每一行的单元格极为缓慢。 (但是,没有其他用户遇到了同样的问题。)

该异常文本如下。是否有人可以审查,并告诉我,如果它引起的一些东西,我的code做错了或者一些与用户的特定设置谁经历过这样的异常不兼容?

我注意到下面的描述说内存已损坏。这是否意味着该用户的计算机有坏的RAM ???

  **************异常文本**************
  System.AccessViolationException:尝试读取或写入受保护的内存。这通常表明其他内存已损坏。
     在System.Drawing.SafeNativeMethods.Gdip.GdipDrawRectangleI(HandleRef图形,HandleRef笔的Int32 X,的Int32 Y,的Int32宽度,高度的Int32)
     在System.Drawing.Graphics.DrawRectangle(笔笔的Int32 X,的Int32 Y,的Int32宽度,高度的Int32)
     在System.Windows.Forms.ControlPaint.DrawFlatCheckBox(图形显卡,长方形矩形,颜色前景,刷背景,ButtonState州)
     在System.Windows.Forms.ControlPaint.DrawFlatCheckBox(图形显卡,长方形矩形,ButtonState州)
     在System.Windows.Forms.ControlPaint.DrawCheckBox(图形显卡,的Int32 X,的Int32 Y,的Int32宽度,高度的Int32,ButtonState州)
     在System.Windows.Forms.ControlPaint.DrawCheckBox(图形显卡,长方形矩形,ButtonState州)
     在System.Windows.Forms.CheckedListBox.OnDrawItem(DrawItemEventArgs E)
     在System.Windows.Forms.ListBox.WmReflectDrawItem(消息&安培;米)
     在System.Windows.Forms.ListBox.WndProc(消息&安培;米)
     在System.Windows.Forms.CheckedListBox.WndProc(消息&安培;米)
     在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息&安培;米)
     在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&安培;米)
     在System.Windows.Forms.NativeWindow.Callback(IntPtr的的HWND,味精的Int32,IntPtr的WPARAM,IntPtr的LPARAM)
 

解决方案

如果没有更多的code,很难确切地说。这里有一些事情要注意。

.NET是托管环境,以及它的基本原则之一是验证code在编译时的能力。具体来说,这意味着某些保证可以由大约code进行单元,如:

  • 不能读取超出数组的边界
  • 无力改变函数指针
  • 不能读取/修改内存code段
  • 无力混淆对象引用的类型

尝试做这些要么在编译时失败,或者有异常的运行时间。

您现在看到的异常来为不安全code的结果。不安全的是一个有点用词不当真的 - 这是更好地描述为无法证实。有时候,出于性能的考虑,有必要放弃code中的核查通过的东西换取原始速度像指针运算。

的WinForms广泛使用的不安全code。这是更确切的说,你的程序集没有任何不安全code,但它取决于库code,它是不安全的。

坏RAM是一种可能,但不太可能。内存损坏时预计的值有没有实际存在。这可能是由于硬件故障,也能软件错误。此异常通常是提高响应的软件错误,在我的经验。另外有消息称该内存的也许的损坏。

该堆栈跟踪实际上可能不是很有见地在这里,因为内存很可能已被破坏,在一些较早的时间,你在这里看到的堆栈帧中只检测到。

这很可能是罪魁祸首。您是否尝试过使用管理Window.Show方法来代替?这可能是因为你的窗口没有手柄还,或者说,它的变化,或任何数目的东西去因为这个错误。一般尽量采用对原生的东西上面托管的包装时要避免的PInvoke。

不幸的是,没有看到你更多的code,这是不可能提供更多的有用的答案,但希望上面提供了一些上下文到异常,并可以帮助你找出你的应用程序在做得到的WinForms进入此状态

The application itself is 2000 lines long so it wouldn't make sense to paste the code here, especially since the exception that one of the users received does not give any hints as to what part of my code is causing the problem.

The app, by the way, is just a Windows Form with a datagridview that typically displays no more than a few hundred rows of data, and some other controls. Before it crashed, it was loading the cells of each row of the datagridview extremely slowly. (But no other user has experienced the same problem.)

The exception text is below. Can someone please review it and tell me if it's caused by something that my code is doing wrong or perhaps something incompatible with the particular setup of the user who experienced this exception?

I notice that the description below says that memory is corrupt. Does it mean that the user's computer has bad RAM???

  ************** Exception Text **************
  System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
     at System.Drawing.SafeNativeMethods.Gdip.GdipDrawRectangleI(HandleRef graphics, HandleRef pen, Int32 x, Int32 y, Int32 width, Int32 height)
     at System.Drawing.Graphics.DrawRectangle(Pen pen, Int32 x, Int32 y, Int32 width, Int32 height)
     at System.Windows.Forms.ControlPaint.DrawFlatCheckBox(Graphics graphics, Rectangle rectangle, Color foreground, Brush background, ButtonState state)
     at System.Windows.Forms.ControlPaint.DrawFlatCheckBox(Graphics graphics, Rectangle rectangle, ButtonState state)
     at System.Windows.Forms.ControlPaint.DrawCheckBox(Graphics graphics, Int32 x, Int32 y, Int32 width, Int32 height, ButtonState state)
     at System.Windows.Forms.ControlPaint.DrawCheckBox(Graphics graphics, Rectangle rectangle, ButtonState state)
     at System.Windows.Forms.CheckedListBox.OnDrawItem(DrawItemEventArgs e)
     at System.Windows.Forms.ListBox.WmReflectDrawItem(Message& m)
     at System.Windows.Forms.ListBox.WndProc(Message& m)
     at System.Windows.Forms.CheckedListBox.WndProc(Message& m)
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
     at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
解决方案

Without more code, it's hard to say exactly. Here are some things to note.

.NET is a managed environment and one of its founding principles is the ability to validate code at compile time. Specifically, this means that certain guarantees may be made about a unit of code, such as:

  • Inability to read beyond the bounds of an array
  • Inability to modify function pointers
  • Inability to read/modify code segments of memory
  • Inability to confuse the type of an object reference

Attempting to do these will either fail at compile time, or at run time with an exception.

The exception you're seeing comes as a consequence of 'unsafe' code. Unsafe is a bit of a misnomer really -- it is better described as 'unverifiable'. Sometimes, for performance reasons, it's necessary to give up the verifiability of code in exchange for raw speed through things like pointer arithmetic.

WinForms makes extensive use of 'unsafe' code. It's truer to say that your assembly doesn't have any unsafe code, but that it depends upon library code that is unsafe.

Bad RAM is a possibility, but quite unlikely. Memory is corrupt when the values expected to be there are not actually there. This can be due to hardware fault, but also to software errors. This exception is usually raised in response to software errors in my experience. Also the message says that memory might be corrupt.

The stack trace may not actually be very insightful here, as the memory is likely to have been corrupted at some earlier time, and was only detected during the stack frame you see here.

This may well be the culprit. Have you tried using the managed Window.Show method instead? It may be that your window doesn't have a handle yet, or that it changes, or that any number of things went wrong because of this. Generally try to avoid PInvoke when using managed wrappers on top of native stuff.

Unfortunately without seeing more of your code, it's impossible to provide a more useful answer, but hopefully the above provides some context onto the exception and may help you find out what your application is doing to get WinForms into this state.

这篇关于这是什么意思时,由于System.AccessViolation异常.NET应用程序崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 22:51