How to disable optimizations during debugging
Sooner or later you may run into a situation where you need to evaluate a local variable under debugger and all you get is this:
"Cannot obtain value of local or argument 'whatever' as it is not available at this instruction pointer, possibly because it has been optimized away'.
Well, it turns out there are two different tricks to solve such problems:
1.
Shawn Burke blogs about How to disable optimizations when debugging Reference Source. In a nutshell, you need to:
- Start VS with the Environment Variable COMPLUS_ZapDisable=1
- Disable the VS Hosting Process (.vshost.exe) before you start debugging
2.
Another tip is from our VB IDE Dev Jared Parsons: Disabling JIT optimizations while debugging. Essentially, Jared points to create an .ini file with the same name as the application's .exe:
[.NET Framework Debugging Control]
GenerateTrackingInfo=1
AllowOptimize=0
He also points to the MSDN article https://msdn.microsoft.com/en-us/library/9dd8z24x.aspx (Making an Image Easier to Debug).
To be frank, this tip didn't work for me for some reason, but I guess it's still worth mentioning.
Hope this helps!
Comments
Anonymous
February 23, 2011
The second suggestion worked for me. I was debugging a WCF service hosted as a windows service. Attaching to the process was causing the same error. I uninstalled the service in question. Created a new {myProjectName}.ini with the text editor of your choice with the above section and settings in the same folder as the exe which was installed. From there attaching to the process and breaking in no longer throws the "Cannot obtain value..." error.Anonymous
January 30, 2013
Attaching to the process always shows the error for IIS hosting WebApp, after i debug with F5 for the Hosted WebApp, local variable can be evaluated.Anonymous
December 22, 2014
Second one worked for me. Thank you.Anonymous
April 02, 2016
For the INI, you need to make a separate .ini for each assembly you want to disable optimizations for. E.g. If you want to debug MyAssembly.dll in the EXE, make a MyAssembly.ini file.