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

I'm hoping someone can enlighten me as to what could possibly be causing this error:

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

I cannot really post code because this error seems to get thrown in any random area of the application. The application will run anywhere from 12-48 hours before throwing the error. Sometimes it will stop in a seemingly random spot and throw the above error, other times the entire application stops and I get a screen with an error that says something along the lines of "There was a fatal error in... This may be a bug in the CLR or..." something about PInvoke or other non relevant info. When this happens all threads show terminated and there is no debugging information available.

In a nutshell this is what the application does:

Its a multi-threaded server application written in entirely in C#. Clients connect to the server via socket. The server runs a virtual "environment" for the clients where they can interact with each other and the environment. It consumes quite a bit of memory but I do not see it leaking. It typically consumes about 1.5GB. I dont think its leaking because the memory usage stays relatively constant the entire time the application is running. Its constantly running code to maintain the environment even if the clients are not doing anything. It uses no 3rd party software or other APIs. The only outside resources this application uses is socket connections and SQL database connections. Its running on a 64bit server. I have tried debugging this in VS2008 & VS2010 using .net 2.0, 3.5, and 4.0 and on multiple servers and the problem still eventually occurs.

I've tried turning off compiler optimizations and several microsoft hot-fixes. Nothing seems to make this issue go away. It would be appreciated if anyone knows any possible causes, or some kind of way to identify whats causing the problem.


Solution 1:

I have just faced this issue in VS 2013 .NET 4.5 with a MapInfo DLL. Turns out, the problem was that I changed the Platform for Build from x86 to Any CPU and that was enough to trigger this error. Changing it back to x86 did the trick. Might help someone.

Solution 2:

I also faced this issue with Visual Studio(VS) 2010. More interestingly, I had several projects in my solution (Console application, WPF application, Windows Forms application) but it was failing only when, I was setting the "Console Application" type of project as start up project of the solution(Even for those which had literally no code or any additional assemblies referred apart from the default ones which come with project template itself).

Following change finally helped me nail down the issue: Go to project properties of the console application project (Alternatively, select project file in solution explorer and press Alt + Enter key combination) -> Go to Debug tab -> Scroll to Enable Debuggers section in right pane -> Check the Enable unmanaged code debugging check box as shown in the snapshot below -> Click Floppy button in the toolbar to save project properties. Root cause of why it happened is still not known to me. Only thing I observed was that there were lot of windows updates which had got installed on my machine the previous night which mostly constituted of office updates and OS updates (More than a dozen KB articles).

enter image description here

Update: VS 2017 onward the setting name has changed as shown in the screenshot below:

enter image description here