views:

111

answers:

3

I am using Visual Studio 2010 in debug mode and I have "optimize code" unchecked. I can't quick watch (or hover on) any variable in the debugger. I get this error "Cannot evaluate expression because the code of the current method is optimized".

Even a line like: int i = -3, doing a quick watch on i, I get "Cannot obtain value of local or argument 'i' as it is not available at this instruction pointer, possibly because it has been optimized away."

This link referenced in a similar question doesn't seem to apply.

Is there a setting I am missing?

+1  A: 

It sounds like you are debugging an optimised / release build, despite having the optimised box un-checked. Things you can try are:

  • Do a complete rebuild of your solution file (right click on the solution and select Rebuild all)
  • While debugging open up the modules window (Debug -> Windows -> Modules) and find your assembly in the list of loaded modules. Check that the Path listed against your loaded assembly is what you expect it to be, and that the modified timestamp of the file indicates that the assembly was actually rebuilt.
  • The modules window should also tell you whether or not the loaded module is optimised or not - make sure that the modules window indicates that it is not optimised.

If you cant't see the Modules menu item in the Debug -> Windows menu then you may need to add it in the "Customise..." menu.

Kragen
The solution is rebuilt. The modules window shows that the assembly is optimized. I have Optimize checkbox unchecked so I don't know why the assembly is always optimized. The solution has a web project but the code I am looking at is in a class library project in debug mode.
Tony_Henrich
@Tony - Silly question, but does the class library have the "Optimised" checkbox de-selected?
Kragen
Yes. I mentioned this fact in my question. I already answered my own question.
Tony_Henrich
+1  A: 

Apart from @Kragen mentioned, If you are debugging a web project

close the visual studio and try deleting the temporary files at C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files

geeth
The solution has a web project but the code is in class library project. I have deleted the temporary files many times.
Tony_Henrich
A: 

While the project was in debug mode, the solution was not. When I changed it, it worked.

Tony_Henrich