Friday, January 22, 2016

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

Problem

While debugging in Visual Studio 2015, many variables are not available. When you try to see a variable's value, Visual Studio tells you, "Cannot obtain value of the local variable or argument because it is not available at this instruction pointer, possibly because it has been optimized away."

Solution

Change the "Solution Configurations" drop down to "Debug".








Explanation

According to Microsoft: "In Release, some locals will be optimized away and will not be available during debugging."




This is done for performance reasons. You can read Microsoft's excellent post on the issue here.

Other things to try

  • Uncheck Project Properties > Build > Optimize Code. (For Visual Studio versions prior to 2015)
  • "In Visual Studio 2015 this option is off by default but if you must debug an application compiled Release, you can turn it back on under Debug -> Options and check “Suppress JIT optimizations on module load (Managed only)”." - MSDN Blog