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?
When you see the "Cannot evaluate expression because the code of the current method is optimized." message after issuing a
Debugger.Break()
statement then please make sure you press F10 to step to the next statement.Once stepped to the next statement, and assuming you are running a Debug build, this message should disappear.
I had mixed c++/cli mfc extension dlls, that were optimised even if debug configuration (seen from VS 2017 Modules window). As previous answer suggested I changed "In VS2013 go to: Tools -> Options -> Debugging -> General and enable 'Use managed compatibility mode'. This disables the new function evaluation behavior." That settings find also in VS 2017.
But that was not enough, so I also copied the UseDebugLibraries setting from another MFC app's project file to extension dll project file.
Then rebuild and that fixed the problem.
If you're trying to debug an ASP.NET project, ensure that the project's Properties > Web > Servers dropdown is set to "IIS Express" (in addition to checking everything else here).
Try to run in debug mode.If you are running in release mode you will get this message.
I had the same problem while debugging a class library from a testbed web app. I was referencing the release version in the testbed and that was set to be optimised in the class library properties.
Un-ticking the optimise code checkbox for the release version in the class library properties, just while I am writing it, has solved the issue.
It sounds like you are debugging an optimised / release build, despite having the optimised box un-checked. Things you can try are:
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.