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).
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
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.
How do you go about logging with Enterprise Library with the following line Logger.Write("Hello
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?
thanks
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.