System.AccessViolationException was unhandled Message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Tags

,

Hmmm, there is a first time for everything.  Access violation exception sounds as if illegal access to some resource were attempted.  Anyway, I googled and binged the exception and wow, I am not alone!  In a brief sentence this sort of exception is not supposed to occur in managed applications according to a post read from MSDN .  I followed the recommendation and Tools->Options :: Debugging->General :: uncheck option “Suppress JIT optimization on module load”; well that did not fix it for me.  As any developer would do, I launched Proc Monitor to observe access request failures to native resources.  First, I set a break point exactly at the line where the exception gets thrown; second, I filtered my Proc Monitor view to display only information pertaining to the process and its child processes being debugged.  Next, I launch the app to run it and monitor all its activities.  Proc Monitor, hmmm, you know what, generic code sure has its overhead;  there is a lot of resource information querying, and resource discovering failure, basically when the CLR tries to located those dlls.  Anyway, Proc Monitor did not find anything useful for my current issue at hand.  I started thinking, what could be wrong with this piece of code that is making an invocation to an external 3rd party lib? one thing for sure, I can not play  around with the [MethodImp] attribute on a third party lib for which I do not have the source code :) . hmmm, since the advice on MSDN mention JIT optimization, and I recall projects’ properties as having an optimize options, perhaps toggling it on and off should not blow off my dev machine :) ;  Element of surprise, yes indeed, making sure the optimize code option inside the project’s Build tab is checked fixed it.  Isn’t that option supposed to be checked by default?  It seems like I unchecked it not too long ago when I was unable to get access to local variables during debug sessions.  but that did not raise the exception then.  Is that a bug in VS ??  Anyway, back to enjoying worriless times writing great code!

Follow

Get every new post delivered to your Inbox.