c# - 访问冲突 : Attempted to read or write protected memory

标签 c# winforms debugging exception-handling access-violation

我有一个 c# (.net 4.0) winforms 应用程序,几乎每个工作日都在 XP SP 3 上运行,每天运行 8 小时。它大部分时间都运行良好,有时运行数月。然后它似乎陷入了困境,每天一次,连续几天,在不同的时间,出现访问冲突异常。我试过查看转储文件,并捕获访问冲突异常以查看堆栈;无论哪种方式,我得到的堆栈几乎相同:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
   at System.Windows.Forms.ToolTip.WndProc(Message& msg)
   at System.Windows.Forms.ToolTip.ToolTipNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)

我很难解决这个问题,因为堆栈跟踪不是很有用。首先,我什至不确定我是否可以信任堆栈跟踪:程序是否到达那里(看起来它正在尝试显示一些工具提示,这当然是可能的)因为内存已经损坏,或者程序是否真的应该合法地在那里,但一些数据存储器已损坏。其次,假设堆栈跟踪是正确且值得信赖的,我看不出有什么方法可以弄清楚是什么破坏了内存……我们没有做任何一致的事情来触发访问冲突……应用程序日志没有显示任何内容其他在此之前捕获的异常...事件日志不会在访问冲突的同时显示任何条目...关于如何进一步诊断此问题的任何提示?

2011 年 10 月 11 日更新:我已经捕捉到异常,但围绕 Application.Run() 方法。到那时,再做很多事情似乎都为时已晚。以防万一由于硬件/驱动程序故障而发生此异常并且并不表示应用程序的内存已损坏 - 是否还有其他任何地方我可以捕获异常(并显示它,然后让应用程序继续)?

2012-03-04 更新:我再次遇到异常,这次是在显示一个相当普通的表单(仅包含一个文本框和一个确定按钮)之后。我正在使用 TextBox.AppendText()。我只是碰巧正在浏览这个 comment同时。 AppendText() 会导致问题吗?当“原始”访问冲突发生时,它们往往会在显示包含我也调用 AppendText() 的富文本框的表单之后发生。情节变厚了!

2012-03-06 更新:我删除了 AppendText 并只使用 TextBox.Text = 代替,但我今天再次遇到访问冲突异常。因此,AppendText 似乎不是罪魁祸首。此外,异常发生在运行 Windows 7 的开发箱上。因此,异常似乎不是特定于 Windows XP 或其他计算机(如内存问题)。

最佳答案

感谢 this post,我能够重现这个问题.因此,一种解决方法似乎是使用 DataGridView.ShowCellToolTips = false; 禁用所有 datagridview 中的所有工具提示;然而,这并不理想。 A better work-around就是打电话

Application.EnableVisualStyles();

before any controls are created in the app .

我已确认无论 DataGridView 是否显示自定义工具提示(使用 CellToolTipTextNeeded)都会出现此问题。

关于c# - 访问冲突 : Attempted to read or write protected memory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7458915/

相关文章:

c# - LabView 应用程序中引用的 C# .dll 是否可以访问 app.config 文件?

c# - 将 bool 属性绑定(bind)到 WinForm 的 BackColor 属性

ruby-on-rails - Rails 脚注部分计数始终为零

iPhone : Simple UITableViewController crash without console error nor debugging clue

c# - 在 C# 中将数组序列 T myArray[] 转换为 IEnumerable<T>

c# - ASP.Net Core DI 的策略模式

c# - 在 INotifyPropertyChanged 时调用函数 - UWP c#

c# - 如何创建继承自TextBox的类

c++ - 运行 WinForm 的函数 "In Background"(VS2010 c++)

python - 在 Atom 中编写 Python 代码时,是否有一种巧妙的方法来调试 Python 代码?