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

C#Visual Studio-2010Debugging

C# Problem Overview


Visual Studio 2010 kills (there is no other word) data in one of the arguments of the function in the unsafe block. What could cause this error? The following message shows by the debugger.

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

C# Solutions


Solution 1 - C#

Go to Project Properties and under Build Make sure that the "Optimize Code" checkbox is unchecked.

Also, set the "Debug Info" dropdown to "Full" in the Advanced Options (Under Build tab).

Solution 2 - C#

Also In VS 2015 Community Edition

go to Debug->Options or Tools->Options

and check Debugging->General->Suppress JIT optimization on module load (Managed only)

Solution 3 - C#

If you compile with optimizations enabled, then many variables will be removed; for example:

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

here the local variable value would typically get removed, keeping the value on the stack instead - a bit like as if you had written:

DoSomething(GetValue());

Also, if a return value isn't used at all, then it will be dropped via "pop" (rather than stored in a local via "stloc", and again; the local will not exist).

Because of this, in such a build the debugger can't get the current value of value because it doesn't exist - it only exists for the brief instant between GetValue() and DoSomething(...).

So; if you want to debug... don't use a release build! or at least, disable optimizations while you debug.

Solution 4 - C#

In visual Studio 2017 goto Debug->Option then check Debugging->general-> and check this option

relevant Visual Studio 2017 Options

Solution 5 - C#

I just ran into this and I was running under Release build configuration instead of Debug build configuration. Once I switched back to Debug my variable showed in the watch again.

Solution 6 - C#

When I was faced with the same problem I just had to clean my solution before rebuilding. That took care of it for me.

Solution 7 - C#

I have faced the same issue and the solution for me is change Solution Configuration from Release to Debug. Hope it helps

Solution 8 - C#

For web applications there is another issue which is important and it is selecting correct configuration during application publish process.

You may build your app in debug mode, but it might happen you publish it in release mode which omptimzes code by default but IDE may mislead you since it shows debug mode while published code is in release mode. You can see details in below snapshot: enter image description here

Solution 9 - C#

Regarding the problem with "Optimize code" property being UNCHECKED yet the code still compiling as optimized: What finally helped me after trying everything was checking the "Enable unmanaged code debugging" checkbox on the same settings page (Project properties - Debug). It doesn't directly relate to the code optimization, but with this enabled, VS no longer optimizes my library and I can debug.

Solution 10 - C#

In my case, I was working on a web api project and although the project was set correctly to full debug, I was still seeing this error every time I attached to the IIS process I was trying to debug. Then I realized the publish profile was set to use the Release configuration. So one more place to check is your publish profile if you're using the 'Publish' feature of your dotnet web api project.

Solution 11 - C#

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.

Solution 12 - C#

As an additional answer for those experiencing this issue when debugging an Azure websites' web app:

When deploying from GitHub, for example, the code is compiled in Azure server optimized by default.

I tell the server to compile in a debuggable way by setting SCM_BUILD_ARGS to /p:Configuration=Debug

but there are more options. See this: http://azure.microsoft.com/blog/2014/05/08/introduction-to-remote-debugging-on-azure-web-sites-part-3-multi-instance-environment-and-git/

Solution 13 - C#

Had the same issue before with a WPF application and all the solutions here did NOT solve the issue. The problem was that the Module was already optimized so the previous solutions DO NOT WORKS (or are not enough to solve the issue):

  • "Optimize Code" checkbox un-Checked
  • "Suppress JIT optimization on module load" checked
  • Solution configuration on DEBUG

The module is still loaded Optimized. See following screenshot: Optimized module


To SOLVE this issue you have to delete the optimized module. To find the optimized module path you can use a tool like Process Hacker.

Double click your program in the "Process panel" then in the new window open the tab ".NET Assemblies". Then in the column "Native image path" you find all Optimized modules paths. Locate the one you want to de-optimize and delete the folder (see screenshot below): enter image description here (I blurred my company name for obvious reasons)

Restart your application (with check box in step 1 correctly checked) and it should works.

Note: The file may be locked as it was opened by another process, try closing Visual Studio. If the file is still locked you can use a program like Lock Hunter

Solution 14 - C#

Check to see if you have a Debuggable attribute in your AssemblyInfo file. If there is, remove it and rebuild your solution to see if the local variables become available.

My debuggable attribute was set to: DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints which according to this MSDN article tells the JIT compiler to use optimizations. I removed this line from my AssemblyInfo.cs file and the local variables were available.

Solution 15 - C#

In Visual Studio 2012:

Go to the project properties -> Debug -> Uncheck "Enable the Visual Studio hosting process"

Solution 16 - C#

In Visual Studio 2017 or 2015:

Go to the Solution right click on solution then select Properties-> select all the Configuration-> Debug then click OK. After that Rebuild and Run,this solution worked for me.

Solution 17 - C#

I had the same issue. Tried all the above and found I also had to delete everything inside {PROJECT_ROOT}\bin\Release\netcoreapp2.2 and {PROJECT_ROOT}\obj\Release\netcoreapp2.2 for my project. Its definitely releated to publishing because although I use Deployment tools / bitbucket on my Azure Web App, I did try the Build >> Publish >> Publish to Azure because I wanted to inspect which files were actually deployed.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncuriousityView Question on Stackoverflow
Solution 1 - C#KarthikView Answer on Stackoverflow
Solution 2 - C#xyq.384.bView Answer on Stackoverflow
Solution 3 - C#Marc GravellView Answer on Stackoverflow
Solution 4 - C#Zahid HasanView Answer on Stackoverflow
Solution 5 - C#JabberwockyDecompilerView Answer on Stackoverflow
Solution 6 - C#CJeView Answer on Stackoverflow
Solution 7 - C#Hoang TranView Answer on Stackoverflow
Solution 8 - C#VSBView Answer on Stackoverflow
Solution 9 - C#MilošView Answer on Stackoverflow
Solution 10 - C#user8683595View Answer on Stackoverflow
Solution 11 - C#Derreck DeanView Answer on Stackoverflow
Solution 12 - C#diegosaswView Answer on Stackoverflow
Solution 13 - C#Ludovic FeltzView Answer on Stackoverflow
Solution 14 - C#NickView Answer on Stackoverflow
Solution 15 - C#MilanaView Answer on Stackoverflow
Solution 16 - C#KalyanView Answer on Stackoverflow
Solution 17 - C#AndyView Answer on Stackoverflow