尝试读取或写入受保护的内存

时间:2009-02-27 20:07:09

标签: c# .net access-violation servicepacks

我开始在我的应用程序中看到一个AccessViolationException抛出了几个不同的位置。它从未出现在我的开发PC上,我们的测试服务器。它也仅在我们的2个生产服务器中的一个上显示出来。因为它似乎只发生在我们的一个生产服务器上,所以我开始在服务器上查看已安装的.net框架版本

我发现(出于某些奇怪的原因),出现问题的生产服务器有2.0 sp2,3.0 sp2和3.5 sp1,而另一台生产服务器和测试服务器有2.0 sp1。

我的应用程序仅针对2.0框架,决定从生产服务器卸载所有框架版本并仅安装2.0 sp1。到目前为止,我还没有能够重现这个问题。很有意思。

开发pc:紧凑型2.0 sp2,紧凑型3.5,2.0 sp2,3.0 sp2,3.5 sp1 测试服务器:2.0 sp1 生产服务器1:2.0 sp1 生产服务器2:2.0 sp2,3.0 sp2,3.5 sp1

现在,为什么我无法在我的开发电脑上重现问题,它上面有2.0 sp2,我无法弄清楚。我听说有传言说这种访问违规可能发生在一些使用远程处理的软件上,我的确如此,但是当实际进行远程处理时,访问冲突永远不会发生。我现在只使用2.0 sp1,但我真的很想知道是否有人遇到过这个问题,如果他们找到了更新版本的frameowork的解决方法。

以下是一些例外情况及其堆栈跟踪:

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)

14 个答案:

答案 0 :(得分:12)

我遇到了同样的问题。 2.0工作正常。安装最多3.5 sp1后,应用程序将获得访问冲突。

已安装http://support.microsoft.com/kb/971030并且我的问题已解决,即使我没有使用LCG。

答案 1 :(得分:11)

微软还发布了一个修补程序(2007年7月2日),以防止一直困扰.NET 2.0平台的错误“试图读取或写入受保护的内存”。看看http://support.microsoft.com/kb/923028 - 不确定它是否适用于您,但您认为可能需要查看它。

答案 2 :(得分:10)

从.NET 4.5升级到.NET 4.5.1后,我遇到了同样的问题。为我修复的是运行此命令:

netsh winsock reset

答案 3 :(得分:3)

对于VS 2013,.NET Framework 4.5.1 also has a AccessViolationException bug(KB2915689)处理SQL Server / TCP套接字时。升级到.NET Framework 4.5.2可以解决此问题。

报告VS.NET AccessViolationException

  

尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

答案 4 :(得分:1)

我正在使用OLEDB,我切换到SQL客户端,它解决了我的问题这个错误。

答案 5 :(得分:1)

在某些WinForms上调用“Show”方法时,我通常会“尝试读取或写入受保护的内存”。我检查过,这些表格没有任何特别之处。我不知道为什么这有效(也许有人可以告诉我)但通常将在表单的“加载”事件中执行的代码移动到“已显示”事件为我修复它并且我再也看不到它了。

答案 6 :(得分:1)

检查以确保线程中没有线程。这就是导致我这个错误的原因。请看这个链接:Attempted to read or write protected memory. This is often an indication that other memory is corrupt

答案 7 :(得分:1)

在某些情况下,在VB.NET中添加“Option Strict On”并通过适当的转换解决它找到的所有问题已经为我解决了这个问题。

答案 8 :(得分:1)

问题可能是由于项目中的混合构建平台DLL。即您将项目构建到任何CPU,但在已为x86平台构建的项目中有一些DLL。由于32位和64位架构的不同内存映射,这些将导致随机崩溃。如果所有DLL都是为一个平台构建的,则可以解决问题。为了安全起见,尝试使用32位x86架构进行扩展,因为它是最兼容的。

答案 9 :(得分:0)

在我的情况下,当我在IIS服务器中为应用程序池设置“启用32位应用程序”= True时,这已得到修复。

答案 10 :(得分:0)

在我的情况下,系统中未安装其中一个共享库中使用的字体。

答案 11 :(得分:0)

在我的情况下,我在添加对我的COM DLL的引用时遇到了“环境变量”的问题。

当我添加对项目的引用时,我正在寻找 P:\ Core 路径,而我之前已将 c:\ core 路径添加到路径中环境可变。

所以我的代码首先尝试了错误的路径。我删除了它并取消注册了DLL引用并使用(regsvr32)重新注册了我的DLL引用。希望这会有所帮助。

答案 12 :(得分:0)

我在创建导致此问题的新项目时忘记添加 EntityFramework。

答案 13 :(得分:-1)

您好有两个可能的原因。

  1. 我们有非托管代码,我们从托管代码调用它。这阻止了运行此代码。 尝试运行这些命令并重新启动电脑

    cmd:netsh winsock reset

  2. 打开Team: FC Ingolstadt 04 , Points: 13 , Diff: -2 Goals: 4 Team: Hamburg , Points: 9 , Diff: -2 Goals: 8 Team: SV Darmstadt 98 , Points: 9 , Diff: -1 Goals: 8 Team: Mainz , Points: 9 , Diff: -3 Goals: 6 Team: FC Augsburg , Points: 4 , Diff: -5 Goals: 7 Team: Werder Bremen , Points: 6 , Diff: -5 Goals: 7 Team: Borussia Moenchengladbach , Points: 6 , Diff: -6 Goals: 9 Team: Hoffenheim , Points: 5 , Diff: -4 Goals: 8 Team: VfB Stuttgart , Points: 4 , Diff: -8 Goals: 9 Team: Schalke 04 , Points: 16 , Diff: 11 Goals: 14 Team: Hannover 96 , Points: 2 , Diff: -12 Goals: 6 Team: Borrusia Dortmund , Points: 16 , Diff: 11 Goals: 15 Team: Bayern Munich , Points: 18 , Diff: 16 Goals: 18 Team: Bayer Leverkusen , Points: 14 , Diff: 3 Goals: 11 Team: Eintracht Frankfurt , Points: 9 , Diff: 4 Goals: 13 Team: Hertha BSC Berlin , Points: 14 , Diff: 1 Goals: 5 Team: 1. FC Cologne , Points: 13 , Diff: 0 Goals: 10 Team: VfB Wolfsburg , Points: 14 , Diff: 4 Goals: 10 并运行命令cmd.exe

    1. 反病毒正在考虑将非托管代码视为有害并限制运行此代码 禁用防病毒,然后检查