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… 😉

You may also like...

18 Responses

  1. Dennis van der Stelt says:

    Unfortunatly, it didn’t work a day later.
    So I still have the problem. Only on my machine, no other machine has it. Sigh

  2. Cephalopod says:

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

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

  4. Dennis van der Stelt says:

    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!

  5. Dennis van der Stelt says:

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

  6. anigalla.net says:

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

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

  8. Elias P Ulahannan says:

    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.

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

  10. XGen SEO says:

    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?

  11. Anjum Rizwi says:

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

    http://anjumrizwi.blogspot.com/2010/08/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

  12. Manoj says:

    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

  13. Himanshu says:

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

  14. So Sereyboth says:

    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.

  15. VJ says:

    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…

  16. Neeraj Tyagi says:

    Great thanks for providing solution… I was dying for this solution..

  17. Jen says:

    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”!

Click on a tab to select how you'd like to leave your comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.