访问冲突:尝试读取或写入受保护的内存

发布于 2024-12-04 21:27:30 字数 2127 浏览 3 评论 0原文

我有一个 ac# (.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()。我碰巧同时浏览了这条评论。 AppendText() 是否会导致问题?当发生“原始”访问冲突时,它们往往发生在显示包含 Richtextbox 的表单之后,我也在该表单上调用了 AppendText()。剧情变厚了!

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

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?

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)?

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!

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

很快妥协 2024-12-11 21:27:30

感谢这篇文章,我能够重复该问题。因此,一种解决方法似乎是使用 DataGridView.ShowCellToolTips = false; 禁用所有 datagridview 中的所有工具提示。然而,这并不理想。 更好的解决方法是调用

Application.EnableVisualStyles();

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

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

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.

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

梦开始←不甜 2024-12-11 21:27:30

我在调用 C dll 的第三方 C# 包装器时发现了此问题。我使用实用程序 editbin.exe /NXCOMPAT:NO {dll name} 禁用了 C# dll 的 DEP,这似乎解决了问题。

当 DEP 打开时,我的 CLR 应该执行额外的检查,并且 C dll 可能正在执行 CLR 认为内存损坏的操作并引发此错误。

您可以在 http://blogs.msdn.com/b/ed_maurer/archive/2007/12/14/nxcompat-and-the-c-compiler.aspx

I've observed this issue when calling into a third party C# wrapper for a C dll. I disabled DEP for the C# dll, using the utility editbin.exe /NXCOMPAT:NO {dll name} and that seems to have fixed the issue.

I CLR should be doing additional checks when DEP is turned on and the C dll might be doing something which the CLR deems as memory corruption and throws this error.

You can read more about this at, http://blogs.msdn.com/b/ed_maurer/archive/2007/12/14/nxcompat-and-the-c-compiler.aspx

太阳男子 2024-12-11 21:27:30

最近,我们在执行 TextBox.AppendText() 时也遇到了 AccessViolationException。在尝试重现该问题后,我们意识到 TextBox 不是问题所在。在我们的例子中,它是拖放功能。

这是一个最小的项目(带有文本框的表单),它将重现该异常:

using System;
using System.Windows.Forms;

namespace TestTextBoxAccessViolation {
    public partial class Form1 : Form {


        public Form1() {
            InitializeComponent();
        }

        private void Form1_DragEnter(object sender, DragEventArgs e) {
            e.Effect = DragDropEffects.Copy;
        }

        private void Form1_DragDrop(object sender, DragEventArgs e) {
            e.Data.GetData("DragImageBits");
            Form1 f = new Form1();
            f.textBox1.Text = "Keep resizing this window and you'll get an AccessViolationException after a while";
            f.Show();
        }
    }
}

结论:不要使用“DragImageBits”。

We recently also got an AccessViolationException when doing TextBox.AppendText(). After trying to reproduce the problem we realized that the TextBox wasn't the problem. In our case it was the drag-and-drop feature.

Here is a minimal project (a Form with a TextBox) that will reproduce the exception:

using System;
using System.Windows.Forms;

namespace TestTextBoxAccessViolation {
    public partial class Form1 : Form {


        public Form1() {
            InitializeComponent();
        }

        private void Form1_DragEnter(object sender, DragEventArgs e) {
            e.Effect = DragDropEffects.Copy;
        }

        private void Form1_DragDrop(object sender, DragEventArgs e) {
            e.Data.GetData("DragImageBits");
            Form1 f = new Form1();
            f.textBox1.Text = "Keep resizing this window and you'll get an AccessViolationException after a while";
            f.Show();
        }
    }
}

Conclusion: Don't use "DragImageBits".

心病无药医 2024-12-11 21:27:30

我发现这个问题不仅在WPF中出现(崩溃),在WinForms中也是如此。我的问题与 OpenFileDialog 有关。
很难说问题的根源是什么,但似乎与 OpenFileDialog 相关的 Microsoft dll 有错误(对我来说,它是 ComDlg32.dll)

我可以调用 ShowDialog() 函数的唯一方法是将其包装在 的帮助下调用

this.BeginInvoke(
        new Action<YourObject, EventArgs>(YourObject_FileDialogOpened), new object[] 
                                                        { YourObjectInstance, e });

事件并在“this”是控件(例如表单)

。 BeginInvoke(...) 保证您的调用将以正确的方式进行处理。

如果您在按钮单击事件或任何其他类似场景下调用 OpenFileDialog,则不会出现问题。

I found that this problem occurs (crash) not only in WPF, but for WinForms. My problem was related to OpenFileDialog.
It is hard to say what is the source of the problem, but still it appears that Microsoft dll related to OpenFileDialog has bugs (for me, it was ComDlg32.dll)

The only way I could call ShowDialog() function was to wrap it in the event and call with the help of

this.BeginInvoke(
        new Action<YourObject, EventArgs>(YourObject_FileDialogOpened), new object[] 
                                                        { YourObjectInstance, e });

where "this" is a Control (for example, Form).

BeginInvoke(...) grants that you call will be process in a proper way.

Problem will not appear if you use call of the OpenFileDialog under button click event or any other similar scenario.

一城柳絮吹成雪 2024-12-11 21:27:30

不确定这是否有任何帮助,但这个问题似乎在旧版本的 .Net 中很常见,微软甚至为此发布了一些修复程序。

初始修复之一如下:

http://support.microsoft.com/kb/923028

这是另一张。
http://support.microsoft.com/kb/975954

Not sure if this will be any help but this problem seem to be common in older versions of .Net and Microsoft even released some fixes for that.

One of the initial fix was as follows,

http://support.microsoft.com/kb/923028

Here is another one.
http://support.microsoft.com/kb/975954

灼疼热情 2024-12-11 21:27:30

这并不容易跟踪/修复,因为所有信息都相当“通用”,因此这些是一些通用指针:

  • 它总是发生在同一台机器上吗?
    如果是,那么可能值得检查机器(内存测试等从可启动的 Linux CD 或类似设备运行)和/或在不同的机器上运行它以查看它是否发生变化...

  • 异常似乎显示工具提示时发生...可能表明显卡驱动程序有问题...选择不同的驱动程序和/或不同的屏幕分辨率等,然后看看会发生什么

  • 您是否使用一些第三方库?
    如果因此检查它们是否存在非托管内存问题(例如使用内存分析器......)可能是值得的。请咨询供应商是否有更新的版本等
    我前段时间遇到过类似的情况,结果是一些第三方库内的非托管内存泄漏(通过内存分析器诊断)...我与供应商核实并得到了一个固定版本,从那时起它就可以顺利运行...

That isn't easy to trace/fix since all information is rather "generic" so these are some general pointers:

  • Does it always happen on the same machine ?
    IF yes, then it could be worth to check the machine (memory tests etc. run from a bootable linux CD or similar) and/or run it on a different machine to see if it changes...

  • The exception seem to happen when displaying a tooltip... could indicate a problem with the graphic card driver... choose a different driver and/or different screen resolution etc. and see what happens

  • Are you using some 3rd-party libraries ?
    IF so it could be worth it to check them for unmanaged memory issues (for example with a memory profiler...). Check with the vendor(s) whether there are newer versions etc.
    I had sometime ago something similar and it turned out to be an unmanaged memory leak inside some 3rd-party library (diagnosed via memory profiler)... I check with vendor and got a fixed version which runs smoothly ever since...

独孤求败 2024-12-11 21:27:30

我和OP有同样的行为。我修改了一个软件并添加了两个 PInvoke 方法(以改进 UI)。不幸的是,我开始收到与OP相同的消息。在查看答案时,我找到了 Raja Hindustani 的答案。注释掉两个 PInvoke 方法后,问题似乎消失了。

I was suffering the same behavior as the OP. I had revised a piece of software and added two PInvoke methods (to improve the UI). Unfortunately, I started receiving the same messages as the OP. In looking over the answers, I found Raja Hindustani's. Upon commenting out the two PInvoke methods, the problem appears to have disappeared.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文