Dennis van der Stelt

If you have one problem and use cache to solve it, you now have two problems.

Community

Email Notifications

News

  • Addicted to Refactor! Pro

I read...

I Use...

Tags

Recent Posts

Archives

Blog Subscription Form

  • Email Notifications
    Go

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… ;-)

Comments

Dennis van der Stelt said:

Unfortunatly, it didn't work a day later.

So I still have the problem. Only on my machine, no other machine has it. Sigh

# June 10, 2009 8:24 PM

Cephalopod said:

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

# June 19, 2009 3:20 PM

Dries Marckmann said:

Dennis,

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.

Good luck!

Dries

# June 22, 2009 3:55 PM

Dennis van der Stelt said:

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!

# June 22, 2009 4:44 PM

Dennis van der Stelt said:

@Dries : That indeed works! Unfortunatly, not everything can be set to x86, for example my Azure / Cloud projects.

# June 29, 2009 10:28 AM

Dennis van der Stelt said:

How do you go about logging with Enterprise Library with the following line Logger.Write("Hello

# July 21, 2009 3:26 PM

anigalla.net said:

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

# August 12, 2009 11:36 AM

Lead Generation said:

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?

thanks

# March 11, 2010 2:20 AM

Elias P Ulahannan said:

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.

# March 16, 2010 5:56 PM

Arun Nagashetti said:

I got the same exception but only on 64 bit platform. here is my experience... archistance.blogspot.com/.../exception-attempted-to-read-or-write.html

# May 5, 2010 11:23 PM

XGen SEO said:

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?

# July 27, 2010 2:32 AM

Anjum Rizwi said:

Eairlier I have faced this issue fixed by doing some changes that is mention here.

anjumrizwi.blogspot.com/.../64-bit-critical-error-has.html

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

VS 2010

.Net 3.5

# October 12, 2010 8:13 AM

Manoj said:

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

# September 2, 2011 11:54 AM

Himanshu said:

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#)

# September 14, 2011 9:03 AM

So Sereyboth said:

in vb.net, i used this code to connect to the device, i got an error:

zkFinger.Connect_Net("192.168.0.201", 4370)

'Error msg:

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

# November 9, 2011 9:48 AM

VJ said:

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

# December 1, 2011 7:56 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 3 and 2 and type the answer here: