16

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);
        }
    }
xeraphim
  • 4,375
  • 9
  • 54
  • 102

4 Answers4

16

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

codekaizen
  • 26,990
  • 7
  • 84
  • 140
  • 3
    i tried hundred other ways.This way worked.weird but thank you. – Rama Krshna Ila Sep 03 '18 at 07:22
  • 4
    I found the following to work without creating a file for every project. In Visual Studio (2019), under Tools menu, Options -> Debugging -> General -> make sure "Suppress JIT optimization on module load (Managed only)" is checked. – SteveB Jun 29 '20 at 19:57
  • @SteveB - yup that sometimes works, but for some reason not all the time. – codekaizen Jun 30 '20 at 19:12
  • Seems this method is really needed when using, say dnspy for example, when debugging production builds. – Chris O Nov 07 '22 at 17:11
4

Go to -> Project Properties -> under Build -> set "Optimize Code" checkbox Unchecked.

Also, Under Project Properties -> Build -> Advanced -> set the "Debug Info" dropdown to "Full" in the Options

isanka thalagala
  • 456
  • 2
  • 10
  • 22
4

For anyone else who was stumped on this, you may be making the same simple mistake I was: my build mode was set to Release from another project I had to build for release earlier in the day. Flip it back to Debug; problem solved.

  • my "active" configuration is set to Release however. It is not shown from the visual studio UI. It is fixed when I changed from the configuration manager. I took this project from someone else so I had no idea why it was set this way. – shrimp rice Oct 09 '19 at 19:07
  • 1
    This issue also occurs when activemode is set to Debug and the "Optimize Code" setting is selected for Debug. @isanka thalagala has an answer posted that addresses this common 'mis-click' – Reahreic Mar 04 '20 at 15:54
2

After making the changes listed by codekaizen and isanka thalagala, do not forget to clean, rebuild and publish to make the changes effective.

Kevin Francis
  • 385
  • 3
  • 10