C# 尝试读取或写入受保护的内存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/596413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Attempted to read or write protected memory
提问by Jeremy
I've started seeing an AccessViolationException being thrown in my application a several different spots. It never occured on my development pc, our test server. It also only manifested itself on 1 of our 2 production servers. Because it only seemed to happen on one of our production servers, I started looking at the installed .net framework versions on the servers.
I found that (for some strange reason), the production server that was having problems had 2.0 sp2, 3.0 sp2, and 3.5 sp1, while the other production server and the test server had 2.0 sp1.
My app only targets the 2.0 framework, decided to uninstall all the framework versions from the production server and install only 2.0 sp1. So far I have not been able to reproduce the problem. Very interesting.
Development pc: compact 2.0 sp2, compact 3.5, 2.0 sp2, 3.0 sp2, 3.5 sp1
Test server: 2.0 sp1
Production server1: 2.0 sp1
Production server2: 2.0 sp2, 3.0 sp2, 3.5 sp1
Now, why I can't reproduce the problem on my development pc which has 2.0 sp2 on it, I can't figure out. I heard rumors that this access violation may happen on some software that utilizes remoting, which mine does, but the access violation never happends when remoting is actually occuring. I'm ok with using only 2.0 sp1 for now, but I'm really interested to know if anyone has had this issue, and if they found a workaround for newer versions of the frameowork.
我已经开始看到在我的应用程序中的几个不同位置抛出了 AccessViolationException。它从未发生在我的开发电脑,我们的测试服务器上。它也只出现在我们的 2 台生产服务器中的 1 台上。因为它似乎只发生在我们的一台生产服务器上,所以我开始查看服务器上安装的 .net 框架版本。
我发现(出于某种奇怪的原因)有问题的生产服务器有 2.0 sp2、3.0 sp2 和 3.5 sp1,而另一个生产服务器和测试服务器有 2.0 sp1。
我的应用程序仅针对 2.0 框架,决定从生产服务器卸载所有框架版本并仅安装 2.0 sp1。到目前为止,我还没有能够重现这个问题。很有意思。
开发电脑:compact 2.0 sp2, compact 3.5, 2.0 sp2, 3.0 sp2, 3.5 sp1 测试服务器:2.0 sp1 Production server1: 2.0 sp1 Production server2: 2.0 sp2, 3.0 sp2, 3.5 sp1
现在,为什么我不能重现这个问题我的开发电脑上有 2.0 sp2,我不知道。我听说有传言说这种访问冲突可能发生在一些使用远程处理的软件上,我的就是这样做的,但是当远程处理实际发生时,访问冲突从未发生过。我现在只使用 2.0 sp1 没问题,但我真的很想知道是否有人遇到过这个问题,以及他们是否找到了适用于较新版本框架的解决方法。
Here's a couple of the exceptions and their stack traces:
以下是一些异常及其堆栈跟踪:
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at ICSharpCode.TextEditor.TextArea.HandleKeyPress(Char ch)
at ICSharpCode.TextEditor.TextArea.SimulateKeyPress(Char ch)
at ICSharpCode.TextEditor.TextArea.OnKeyPress(KeyPressEventArgs e)
at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
at System.Windows.Forms.Control.WmKeyChar(Message& m)
at System.Windows.Forms.Control.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)
System.AccessViolationException: 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.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
at System.Windows.Forms.RichTextBox.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)
采纳答案by chris
I had the same problem. 2.0 worked fine. after installing up to 3.5 sp1, application gets Access Violation.
我有同样的问题。2.0 运行良好。安装最多 3.5 sp1 后,应用程序会出现访问冲突。
installed http://support.microsoft.com/kb/971030and my problem is solved, even though I am not using LCG.
安装了http://support.microsoft.com/kb/971030并且我的问题解决了,即使我没有使用 LCG。
回答by Ash
Microsoft also released a hotfix (July 2nd, 2007) to prevent the error "Attempted to read or write protected memory" that has been plaguing the .NET 2.0 platform for some time now. Look at http://support.microsoft.com/kb/923028- not sure if it applies to you, but thought you might like to check it out.
Microsoft 还发布了一个修补程序(2007 年 7 月 2 日),以防止出现一段时间以来一直困扰 .NET 2.0 平台的错误“尝试读取或写入受保护的内存”。查看http://support.microsoft.com/kb/923028- 不确定它是否适用于您,但您可能想查看一下。
回答by Angela
I was using OLEDB and I switched to SQL Client and it solved my problem with this error.
我正在使用 OLEDB 并切换到 SQL 客户端,它解决了这个错误的问题。
回答by Denis
I usually get "Attempted to read or write protected memory" when calling the "Show" method on some WinForms. I checked and there doesn't appear anything special about those forms. I don't know why this works (maybe someone can tell me) but usually moving the code that gets executed in the "Load" event of the form to the "Shown" event fixes it for me and I never see it again.
在某些 WinForms 上调用“Show”方法时,我通常会得到“尝试读取或写入受保护的内存”。我查了一下,这些表格没有任何特别之处。我不知道为什么会这样(也许有人可以告诉我),但通常将在表单的“Load”事件中执行的代码移动到“Shown”事件为我修复它,我再也看不到它了。
回答by Joe Brunscheon
Check to make sure you don't have threads within threads. That's what caused this error for me. See this link: Attempted to read or write protected memory. This is often an indication that other memory is corrupt
检查以确保线程中没有线程。这就是导致我出现此错误的原因。请参阅此链接:尝试读取或写入受保护的内存。这通常表明其他内存已损坏
回答by Cocowalla
I had the same problem after upgrading from .NET 4.5 to .NET 4.5.1. What fixed it for me was running this command:
从 .NET 4.5 升级到 .NET 4.5.1 后,我遇到了同样的问题。为我修复的是运行此命令:
netsh winsock reset
netsh winsock reset
回答by Denis
In some cases adding "Option Strict On" in VB.NET and resolving all issues it finds by proper casting has solved this problem for me.
在某些情况下,在 VB.NET 中添加“Option Strict On”并解决它通过正确转换发现的所有问题已经为我解决了这个问题。
回答by SliverNinja - MSFT
For VS 2013, .NET Framework 4.5.1 also has a AccessViolationException
bug(KB2915689) when dealing with SQL Server / TCP Sockets. Upgrading to .NET Framework 4.5.2corrects this problem.
对于 VS 2013,.NET Framework 4.5.1AccessViolationException
在处理 SQL Server / TCP 套接字时也有一个错误(KB2915689)。升级到.NET Framework 4.5.2可以解决此问题。
Reported VS.NET AccessViolationException
报告 VS.NET AccessViolationException
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
尝试读取或写入受保护的内存。这通常表明其他内存已损坏。
回答by Santokh SIngh
Hi There are two possible reasons.
您好 有两个可能的原因。
We have un-managed code and we are calling it from managed code. that is preventing to run this code. try running these commands and restart your pc
cmd: netsh winsock reset
我们有非托管代码,我们从托管代码中调用它。这是阻止运行此代码。尝试运行这些命令并重新启动您的电脑
cmd:netsh winsock 重置
open cmd.exe
and run command "netsh winsock reset catalog"
打开cmd.exe
并运行命令"netsh winsock reset catalog"
- Anti-virus is considering un-managed code as harmful and restricting to run this code disable anti-virusand then check
- 防病毒软件将非托管代码视为有害并限制运行此代码 禁用防病毒软件然后检查
回答by Dmitriy Grishin - dogrishin
In my case this was fixed when I set up 'Enable 32 Bit applications'=True for Application pool in IIS server.
在我的情况下,当我为 IIS 服务器中的应用程序池设置“启用 32 位应用程序”=True 时,此问题已得到解决。