Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot obtain value because it has been optimized away

I have a problem with debugging... All of a sudden I can't see the values of most variables while debugging. I've managed to get two different messages in the Immediate Window:

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

and

Internal error in the expression evaluator.

I've tried and checked the following things:

  1. Solution Configuration is set to debug (not release)
  2. Project -> Settings -> Build -> Optimize code is not set
  3. Tools -> Options -> Debugging -> Use Managaed Compatibility Mode (didn't work)

Do you have any further ideas how I can properly debug again? :(

Thanks in advance

Edit The code is nothing special.. it happens when I try to watch what's inside parameter[Key]

public void AddOrUpdateQuartzJob(string jobName, IList<KeyValuePair<string, string>> parameters)
    {
        var jobDetail = this.GetJobDetail(jobName);

        if (jobDetail != null)
        {
            foreach (var parameter in parameters)
            {
                jobDetail.JobDataMap[parameter.Key] = parameter.Value;
            }
        }
        else
        {
            this.ScheduleNewJob(jobName, parameters);
        }
    }
like image 234
xeraphim Avatar asked Oct 14 '14 13:10

xeraphim


People also ask

Why can't I get the value of local or argument?

Cannot obtain value of local or argument as it is not available at this instruction pointer, possibly because it has been optimized away. Show activity on this post. Go to Project Properties and under Build Make sure that the "Optimize Code" checkbox is unchecked.

Why cannot obtain value of local or argument'parameter'in Immediate window?

I've managed to get two different messages in the Immediate Window: Cannot obtain value of local or argument 'parameter' as it is not available at this instruction pointer, possibly because it has been optimized away. Internal error in the expression evaluator.

Why cannot get value of argument parameter in an expression?

Cannot obtain value of local or argument 'parameter' as it is not available at this instruction pointer, possibly because it has been optimized away. Internal error in the expression evaluator. Do you have any further ideas how I can properly debug again? : ( Show activity on this post. Show activity on this post.

Why can't I get the current value of a return value?

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


1 Answers

The best way I've found to convince the JIT compiler not to optimize the code is to use an INI file with the name of the assembly in the same folder the assembly is in with the contents:

[.NET Framework Debugging Control]
GenerateTrackingInfo=1
AllowOptimize=0

Note that it has to be the name of the assembly, not the name of the process EXE (unless it is the EXE assembly you want to debug). E.g. if you have an assembly MyCode.dll the INI file name would be MyCode.ini.

Here are some slides from a presentation on .Net debugging which show the difference:

With Optimization:

Debugging with optimization

Without Optimization:

Debugging without optimization

like image 136
codekaizen Avatar answered Oct 06 '22 00:10

codekaizen