访问冲突:尝试读取或写入受保护的内存 [英] Access Violation: Attempted to read or write protected memory

查看:1743
本文介绍了访问冲突:尝试读取或写入受保护的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#(.NET 4.0)WinForms应用程序运行几乎每周一天,每天8小时,在一个XP SP 3.正常工作的大部分时间,有时几个月。然后,它似乎在一个坏的法术搞定,每天一次,在连续几天,在不同的时间,访问冲突异常出现。我试着看转储文件,捕获访问冲突异常看堆栈;无论哪种方式,我得到几乎相同的堆栈:

I have a c# (.net 4.0) winforms application that runs pretty much every week day, 8 hours a day, on an XP SP 3. It works fine most of the time, sometimes for months. Then it seems to get in a bad spell, and once a day, for a few days in a row, at various times, an access violation exception comes up. I've tried looking at the dump file, and catching the access violation exception to look at the stack; either way, I get pretty much the same stack:

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)

我有一个非常艰难的时间来修复这一点,因为堆栈跟踪不是非常有用。首先,我甚至不知道我是否可以信任堆栈跟踪:没有程序那里(看起来像它试图显示一些提示,这当然是可能的),因为内存已经损坏,或者如果该方案真的应该顺理成章存在,但一些数据存储器已损坏。其次,假设堆栈跟踪是正确的和值得信赖的,我没有看到一个方法来找出是什么破坏内存......我们没有做任何事情一致触发访问冲突...应用程序日志不显示任何其它捕获的异常在那之前......事件日志不会在同一时间访问冲突显示任何条目......就如何进一步诊断呢?

I'm having a very hard time fixing this because the stack trace isn't very useful. First, I'm not even sure if I can trust the stack trace: does the program get there (looks like it's trying to display some tooltip, which is certainly possible) because memory is already corrupted, or if the program really should legitimately be there, but some data memory is corrupted. Second, assuming the stack trace is correct and trustworthy, I don't see a way to figure out what is corrupting the memory... We are not doing anything consistent to trigger the access violation... the application log does not show any other caught exceptions before then... the event logs don't show any entries at the same time as the access violation... Any hints on how to further diagnose this?

更新2011-10-11:我已经捕捉异常,而是围绕Application.Run()方法。在这一点上,似乎为时已晚做多。万一这个异常是由于发生在硬件故障/驱动器,并不表示该应用程序的内存损坏 - 会不会有其他任何地方,我可以捕获该异常(并显示它,但然后让应用程序继续运行)?

Update 2011-10-11: I'm already catching the exception, but around the Application.Run() method. At that point it seems it's too late to do much. Just in case this exception is happening due to faulty hardware/driver and does not indicate that the application's memory is corrupt -- would there be any place else that I could catch the exception (and display it, but then let the application continue)?

更新2012-03-04:我再次得到了异常,这时候显示一个相当琐碎的形式后(只包含一个文本框和一个确定按钮)。我使用TextBox.AppendText()。我只是碰巧在同一时间被浏览此评论。可以AppendText通过()是导致该问题?当原始访问冲突发生时,他们往往会显示一个包含()一个RichTextBox上,我也呼吁AppendText通过一种形式后happend。情节变得

Update 2012-03-04: I got the exception again, this time after displaying a fairly trivial form (only contains a textbox and an ok button). I was using TextBox.AppendText(). I just so happened to be browsing this comment at the same time. Could AppendText() be causing the issue? When the 'original' access violations occur, they tend happend after displaying a form that contains a richtextbox on which I also call AppendText(). The plot thickens!

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

Update 2012-03-06: I removed AppendText and just used TextBox.Text = instead, but I got the access violation exception again today. Thus, AppendText does not seem to be the culprit. Furthermore, the exception happened once on a dev box, running Windows 7. Thus, it does not seem like the exception is specific to Windows XP, or to the other computer (like a memory issue).

推荐答案

我是能够复制的问题感谢这个帖子。所以,有工作的周围出现使用DataGridView.ShowCellToolTips来禁止所有的datagridview所有的提示= FALSE;然而,这是不理想的。 一个更好的解决方法是调用

I was able to duplicate the issue thanks to this post . So, one work-around appears to disable all tooltips in all datagridview using DataGridView.ShowCellToolTips = false; However, this is not ideal. A better work-around is to call

Application.EnableVisualStyles();

在应用在创建任何控件之前。

before any controls are created in the app.

我已经证实,出现该问题在DataGridView是否显示自定义工具提示(带CellToolTipTextNeeded)或没有。

I have confirmed that the issue occurs whether the DataGridView is displaying custom tooltips (with CellToolTipTextNeeded) or not.

这篇关于访问冲突:尝试读取或写入受保护的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆