curiousity
curiousity

Reputation: 4741

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

Upvotes: 297

Views: 191992

Answers (17)

Ludovic Feltz
Ludovic Feltz

Reputation: 11916

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

Upvotes: 1

VSB
VSB

Reputation: 10375

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

Upvotes: 14

Andy
Andy

Reputation: 2354

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.

Upvotes: 0

Kalyan
Kalyan

Reputation: 19

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.

Upvotes: 1

Zahid Hasan
Zahid Hasan

Reputation: 385

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

relevant Visual Studio 2017 Options

Upvotes: 34

Hoang Tran
Hoang Tran

Reputation: 966

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

Upvotes: 10

user8683595
user8683595

Reputation: 61

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.

Upvotes: 6

Milana
Milana

Reputation: 772

In Visual Studio 2012:

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

Upvotes: 0

Nick
Nick

Reputation: 9

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.

Upvotes: 0

xyq.384.b
xyq.384.b

Reputation: 2156

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)

Upvotes: 198

JabberwockyDecompiler
JabberwockyDecompiler

Reputation: 3390

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.

Upvotes: 26

diegosasw
diegosasw

Reputation: 15634

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/

Upvotes: 1

Karthik
Karthik

Reputation: 4684

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

Upvotes: 466

CJe
CJe

Reputation: 1992

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

Upvotes: 9

Miloš
Miloš

Reputation: 557

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.

Upvotes: 6

Derreck Dean
Derreck Dean

Reputation: 3766

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.

Upvotes: 3

Marc Gravell
Marc Gravell

Reputation: 1062865

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.

Upvotes: 62

Related Questions