Attempted to read or write protected memory
Asked Answered
S

14

39

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)
Sidecar answered 27/2, 2009 at 20:7 Comment(2)
The only time I have AccessViolationExceptions were inside of Visual Studio .NET with an old version of ahknsvn installed. Other than that I haven't got a clue... maybe something else is installed on that system, such as PostSharp?Rese
Related post - Attempted to read or write protected memory. This is often an indication that other memory is corruptCommandant
T
13

I had the same problem. 2.0 worked fine. after installing up to 3.5 sp1, application gets Access Violation.

installed http://support.microsoft.com/kb/971030 and my problem is solved, even though I am not using LCG.

Thea answered 9/6, 2010 at 11:44 Comment(1)
Do you have a direct link to the hotfix download? That page says we need to contact Microsoft support but doens't have any download links.Planimeter
A
19

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

Airdrop answered 10/12, 2013 at 10:38 Comment(5)
Thank you! I was having the same problem until I found your solution!Dicky
My project is using .NET 4.0, and this command solved that exception for me, thank you!Beverlybevers
I know this is a long dormant thread, but what on earth prompted you to try that? I see this exception every once in a while in a situation where it is difficult to catch a memory dump to debug. If you see this and have some information, thanks in advance!Airt
@ChrisBilson I don't remember 100%, but I think I'd perhaps tried this thinking it was an issue with a Nortel VPN component, and it happened to work.Airdrop
I don't understand how this problem arose out of nowhere but this solution resolved it for me. Thank you! It does require admin privileges followed by a restart.Cryohydrate
T
13

I had the same problem. 2.0 worked fine. after installing up to 3.5 sp1, application gets Access Violation.

installed http://support.microsoft.com/kb/971030 and my problem is solved, even though I am not using LCG.

Thea answered 9/6, 2010 at 11:44 Comment(1)
Do you have a direct link to the hotfix download? That page says we need to contact Microsoft support but doens't have any download links.Planimeter
D
11

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.

Decennium answered 17/5, 2009 at 23:8 Comment(2)
Does it mean if I change my program's .Net to .Net 3.5, will the problem be solved?Cuttlefish
Correct, the link to download the hotfix from the page that @Decennium linked appears to be broken.Wistful
M
4

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.2 corrects this problem.

Reported VS.NET AccessViolationException

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Morgue answered 30/12, 2014 at 22:40 Comment(3)
I am getting this on 4.6.1 when connecting to an IBM AS400.Illfated
@JosephMorgan I am also getting this with 4.6.1. It happened when trying to export database schema. Did you have any luck resolving it?Daddylonglegs
Still no joy. I get it when a form closes as I quit the application which is using the connection, as well. I wonder if I need to be disposing the connection and not just closing it??Illfated
F
1

I was using OLEDB and I switched to SQL Client and it solved my problem with this error.

Finegrained answered 14/2, 2012 at 17:21 Comment(2)
Nothing here seems to indicate that the TS is using either of those drivers (nor is performing database operations).Curator
I came to this question because it was the highest ranked. For what it's worth, anyone finding this question and response but their problem is with a db query. I had a query working fine SQL CE 3.5 (sp1 and sp2) x86, but failing in x64. Column datatype mismatches on a join, which were being implicitly converted worked in x86 but resulted in the attempted to read or write protected memory error in x64. Wrapping one of the columns with a convert or coalesce solved the problem.Chronometer
K
1

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.

Katricekatrina answered 3/12, 2012 at 20:3 Comment(0)
I
1

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

Ironing answered 8/8, 2013 at 17:27 Comment(0)
K
1

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.

Katricekatrina answered 18/2, 2014 at 23:31 Comment(0)
H
1

Hi There are two possible reasons.

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

open cmd.exe and run command "netsh winsock reset catalog"

  1. Anti-virus is considering un-managed code as harmful and restricting to run this code disable anti-virus and then check
Hindermost answered 20/10, 2015 at 6:29 Comment(1)
This was my issue. +1 I'm not sure why you were downvoted without comments or reason.Stempson
S
1

In my case this was fixed when I set up 'Enable 32 Bit applications'=True for Application pool in IIS server.

Sturrock answered 10/6, 2016 at 10:40 Comment(0)
M
1

The problem may be due to mixed build platforms DLLs in the project. i.e You build your project to Any CPU but have some DLLs in the project already built for x86 platform. These will cause random crashes because of different memory mapping of 32bit and 64bit architecture. If all the DLLs are built for one platform the problem can be solved. For safety try bulinding for 32bit x86 architecture because it is the most compatible.

Mako answered 22/2, 2017 at 4:5 Comment(1)
That was it. My solutions runs perfectly for when built against x86 but gives error on x64.Dentalium
O
0

In my case fonts used in one of the shared library was not installed in the system.

Overcheck answered 6/10, 2016 at 9:32 Comment(0)
S
0

In my case I had trouble with the "Environment variables" while adding reference to my COM DLL.

When I added the reference to my project, I was looking for P:\Core path, whereas I had added the c:\core path in past into path environment varaible.

So my code was attempting wrong path first. I removed that and un-registered the DLL reference and re-registered my DLL reference using (regsvr32). Hope this helps.

Salesin answered 21/2, 2018 at 21:25 Comment(0)
N
0

I forgot to add EntityFramework when creating a new project which caused this problem.

Noncompliance answered 26/3, 2021 at 8:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.