“Cannot evaluate expression because the code of th

2019-01-31 04:34发布

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?

20条回答
Anthone
2楼-- · 2019-01-31 05:17

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.

查看更多
手持菜刀,她持情操
3楼-- · 2019-01-31 05:17

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.

    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
      ...
      <UseDebugLibraries>true</UseDebugLibraries>

Then rebuild and that fixed the problem.

查看更多
不美不萌又怎样
4楼-- · 2019-01-31 05:18

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).

查看更多
Deceive 欺骗
5楼-- · 2019-01-31 05:19

Try to run in debug mode.If you are running in release mode you will get this message.

查看更多
smile是对你的礼貌
6楼-- · 2019-01-31 05:20

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.

查看更多
来,给爷笑一个
7楼-- · 2019-01-31 05:21

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.

查看更多
登录 后发表回答