C# – Attempted to read or write protected memory

access-violationcnetservicepacks

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.

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)

Best Answer

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:

netsh winsock reset

Related Topic