I'm in c# for quite a long time, but I never got this kind of error. First of all, do you see anything wrong(possibly wrong) about this single code block (except it's logic of course, I know it always return 0)?
public static int GetDecimals(MySimpleEnum val)
{
int result = 0;
switch (val)
{
case MySimpleEnum.Base:
case MySimpleEnum.Slow:
case MySimpleEnum.Normal:
case MySimpleEnum.Quick:
case MySimpleEnum.Fastest:
result = 0;
break;
}
return result;
}
Release project settings: DEBUG constant = false; TRACE constant = true; Optimize Code = true; Output/Advanced/Debug info = none;
IIS = version 7.5
This method, having specified release settings throws "System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
And here is the funny part. These are cases, when it does not throw this exception:
- Run it on IIS (Express and not-Express) 8.5 (no project edit needed).
- Set Optimize code = false; and Output/Advanced/Debug info = false;
- Wrapp the method inside to try/catch block (tried also log the exception using log4net in catch block - empty log)
- Replace inner of the method with some different code.
Things to note:
- The exception had to be catched using win debugger on the server (no regular .NET exception handler was called)
- Application crashes after the exception.
- This code block was functioning several months ago (no release for a long time). The error started probably with some update.
- Tested on two different servers with IIS 7.5 and one machine having IIS 8.5 and IIS Express (VS2012). Same results.
- I have tried a lot of different combinations of projects settings. Basicaly if I don't set the settings like in point 2., it throws the exception on IIS 7.5.
- My working (and build) machine is running Windows 8.1 with latest updates.
- The code block is situated in separate project (class library).
I know how to fix this. But I don't like closing issues, while not knowing the cause. Also, I want to avoid this in future. Do you think you could help me with that? If it is some null reference exception, why is this happening? And why it is debug/release specific or IIS version specific?
*Note2:
Recently I had problem with missing dll's (System.Net.Http.Formatting.dll, System.Web.Http.dll, System.Web.Http.WebHost.dll) while publishing. It was because of some Microsoft security update. Maybe this is something similiar.*
Edit 1 Adding structure of the enum declaration.
public enum MySimpleEnum
{
Base = 0,
Slow = 1,
Normal = 2,
Quick = 3,
Fastest = 4
}
Also, I just tried adding [MethodImpl(MethodImplOptions.NoInlining)], but no help.
[MethodImpl(MethodImplOptions.NoInlining)]
? – Ingalls