Visual Studio 2010在不安全块中杀死函数的一个参数中的数据(没有其他词)。 是什么导致了这个错误?调试器显示以下消息。

Cannot obtain value of local or argument as it is not available at this instruction pointer, possibly because it has been optimized away.

当前回答

在Visual Studio 2017或2015中:

转到解决方案,右键单击解决方案,然后选择属性->选择所有配置->调试,然后单击确定。 在“重新构建并运行”之后,这个解决方案对我来说是有效的。

其他回答

也 在VS 2015社区版

去 调试->选项或工具->选项

并检查 在模块负载上抑制JIT优化(仅Managed only)

如果你在编译时启用了优化,那么许多变量将被删除;例如:

SomeType value = GetValue();
DoSomething(value);

在这里,局部变量值通常会被删除,而是保留在堆栈上的值-有点像你写的:

DoSomething(GetValue());

同样,如果一个返回值根本没有被使用,那么它将通过“pop”被删除(而不是通过“stloc”存储在本地,并且再次;本地将不存在)。

正因为如此,在这样的构建中,调试器无法获得value的当前值,因为它不存在——它只存在于GetValue()和DoSomething(…)之间的短暂瞬间。

所以;如果你想调试…不要使用发布版本!或者至少在调试时禁用优化。

I found that I had the same problem when I was running a project and debugging by attaching to an IIS process. I also was running in Debug mode with optimizations turned off. While I thought the code compiled fine, when I detached and tried to compile, one of the references was not found. This was due to another developer here that made modifications and changed the location of the reference. The reference did not show up with the alert symbol, so I thought everything was fine until I did the compilation. Once fixing the reference and running again it worked.

关于“优化代码”属性未选中的问题,但代码仍然编译为优化:在尝试了所有东西后,最后帮助我的是在同一设置页面上检查“启用非托管代码调试”复选框(项目属性-调试)。它与代码优化没有直接关系,但是启用这个功能后,VS不再优化我的库,我可以调试。

当我面临同样的问题时,我只需要在重建之前清理我的解决方案。这帮我解决了问题。