Attempted to read or write protected memory in a .NET application
Now this got me puzzled today for some time. I started up Visual Studio 2008 and opened a solution I had been working on for some time. But when trying to debug the application, it threw an error on Unity and the SerivceLocator trying to get an instance of a class. Not an interface, a class. It had no constructor nor dependency properties or anything weird, but I used Unity because it was supposed to in the future.
Anyway, I got an error from Unity and some inner exception said
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Now that was super weird of course. Weirder even was that all tests, also using Unity, still all succeeded. So I replaced the line of code to instantiate the class by hand and it worked. A few lines further LINQ was used and the same error occurred. So I tried everything from running other applications and opening other VS2008 solutions, doing memory tests, etc, etc. Everything worked fine, but just this one project I was supposed to work on.
I Googled and even Blinged for some time, without finding anything relevant. Until I stumbled upon this thread in the Microsoft forums. There they did not find the error just weird, but wierd even.
Anyway, the solution was to enable JIT optimization. In other words, in Visual Studio 2008 choose “Tools” and then “Options”. Select “Debugging” and “General” and find the line that says “Suppress JIT optimization on module load”. This kind of makes sure that the debugger and the JIT compiled code aren’t running out of sync because the JIT compiler is such a super duper optimizer of your code. This did the trick.
Of course my code is so much optimized it never runs out of sync, but it still didn’t feel good. So I was trying to reproduce the error and turned “Supress JIT optimization” on again. To my even bigger surprise, the error did not return and my application is working just fine again. This even more ensured me turning it on or off on my own code, does not make a difference… 😉
Unfortunatly, it didn’t work a day later.
So I still have the problem. Only on my machine, no other machine has it. Sigh
I’ve been researching a similar problem that affects only certain machines. From what I’ve seen, the error itself is pretty generic, but is often happens when working with something external to the CLR (in my case, print drivers, in a lot of other people’s cases, databases).
Try setting the build configuration of the project to x86. This worked for both Bjorn and Pazcal on their x64 machines using a combination of WPF and Linq2Sql.
Why did I not think of that? Or… maybe I did but it didn’t work. I’ll have a look, thanks for the tip!
@Dries : That indeed works! Unfortunatly, not everything can be set to x86, for example my Azure / Cloud projects.
my friend had the same issue when he is calling the assembly Interop.Excel.dll in two different .net applications.
As long as there is only one application running on the machine, you won’t get any error.
if you start the second application, still you won’t get any error.
But if you open the windows form where in the form load it uses Interop.excel.dll, then it throws an exception
“Attempted to read or write protected memory…. ”
Just re-installed my operating system, visual studio and everything. All of the sudden it keeps giving “Attempting to read or write protected memory in a .net application”. Looks like I have to grant some special rights to specific users? could any one please help?
I got the same problem. My problem was that I compiled the dll with a different version of third party component like Infragistics 9.1 instead of Infragistics 9.2. When I referenced the actual component and compiled then it’s working fine.
I got the same exception but only on 64 bit platform. here is my experience… http://archistance.blogspot.com/2010/05/exception-attempted-to-read-or-write.html
I have the same problem with one of my clients. It works fine with all others. I used a 3rd party POP control to retrieve email, and get this error. I have searched on the net and it seems that there are too many different situations cause this error. My question is: is this a hardware memory issue? or the programming issue? If it is a code issue, why does it happen to all clients?
Eairlier I have faced this issue fixed by doing some changes that is mention here.
However after 2 months I am facing same issue in same area after fixing style cop warning issue.
It is working on locahost 32 bit & win 2008 32 bit mode.
Not working on 64 bit.
My System environment:
Win Server 2008 R2
IIS 7 – 64 bit
I solved this problem in the following web:
I am facing the same problem when i try to save and excel book using .saveas method. I googled for last four days but no suggestion is resulting to be useful.
If i save a workbook using .save method then it gets saved properly. Please i would be very thankful if anybody can help me.
Thanks in advance
I am also getting the same error while saving the excel file using saveas method. Can anyone please help me rectify this problem ? I am using VS 2008 (C#)
in vb.net, i used this code to connect to the device, i got an error:
connect rfid reader Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
I am trying to open an animation in a web browser.. and the same error is thrown when the animation is being loaded…. I have tried changing to x86 but does not solve the issue…
Note : The error occurs only when the animation with .HTML extension is loaded…
Great thanks for providing solution… I was dying for this solution..
I had the exactly same error when I upgraded to VS2010. I did clear, rebuild multiple times, sometimes it will clear that error, but sometimes the error will come back again, very very annoying. But then I tried your trick, it never comes back again, like you said, ” weird even”!