Can I prevent the CLR from optimizing away debugging information?

10,999

Solution 1

I've encountered this same problem before, and it's invariably due to the fact that Debug mode has been turned off in some way. Try checking each of the following:

  1. The current build configuration for the solution and the appropiate project(s) is Debug.
  2. In the Build tab of the property pages, the Optimize code checkbox is unchecked.

If this is all correct, then I recommend you paste the text written to the Output window here so can we can potentially spot any more unusual cause of the issue.

Solution 2

Dont Forget the obvious

Make sure your arent trying to debug your release build. All of these compile settings set behind these configurations. The debug version is one for debugging ;-)

Share:
10,999
Rytmis
Author by

Rytmis

Software developer since 2001, independent consultant since 2010. I thrive on solving tricky problems.

Updated on June 08, 2022

Comments

  • Rytmis
    Rytmis almost 2 years

    I've written an abstract base class for unit tests that sets up just enough environment for our tests to run. The class exposes some of the runtime environment bits as properties whose types vary test by test (the property types are type arguments specified in the inheriting, concrete test class).

    This is all well and good, except a co-worker noticed that he can't view any of the class' properties in the debugger. Turns out the reason is that he had no fields defined in his inheriting class, and the CLR optimized something or other away, so the debugger couldn't display the properties. Is it possible to prevent this in the base class somehow, or do I have to resort to telling everyone they need to define at least one field which is used somewhere during the tests?

    Edit:

    Sounds like a likely culprit should be the optimization/debug settings. That said, I'm building the app from Visual Studio in Debug mode, I've double-checked that all projects are set for a debug build, and none of the projects in this solution have the Optimize flag set.

    Perhaps it would also be relevant to note that I'm using MSTest and the Visual Studio test runner.

    Edit 2:

    By "can't view properties" I'm referring to when I evaluate the property in Quickwatch and get a red exclamation mark and a text "Could not evaluate expression" error text. And lest you think I'm entirely off base with my suspicions, adding an instance field that gets initialized in the test initialize method makes the problem go away...

    Edit 3:

    Checked the build output. I notice that the compiler is invoked with these options:

    /debug+
    /debug:full
    /optimize-
    /define:DEBUG,TRACE
    

    I should think that would be enough to stop this from happening, but there you go. :)

  • Rytmis
    Rytmis almost 15 years
    This sounds likely, yet the active config is debug, the config says "debug" for all projects, and the "optimize" flag for the assembly where the classes are located is not set. I'm physically unable to paste anything here (different machine) as well as not permitted to do so, unfortunately. Anything specific I should be looking for?
  • Noldorin
    Noldorin almost 15 years
    @Rytmis: Could you please clarify what you mean by "he can't view any of the class' properties". Are you using watches/just hovering over the variable names with the mouse? Any message? Regarding the build output, I would just be looking for any anomolies. Now that you've mentioned this is MSTest however, I'm starting to think the problem is something specific to that.
  • Rytmis
    Rytmis almost 15 years
    Added clarification to the problem description: when I open up Quickwatch and type in the name of the property, I get "Could not evaluate expression." The moment I add an instance field to the inheriting class (the one being debugged), this problem disappears.
  • Noldorin
    Noldorin almost 15 years
    @Rytmis: Can you confirm whether you get the same problem debugging normally (as opposed to using MSTest)?
  • Rytmis
    Rytmis over 14 years
    I haven't run into this any more, so I'm assuming I botched something in the rebuild after changing these settings. :)
  • Matt
    Matt over 11 years
    I was experiencing this problem as well, but everything was set to DEBUG. Even did a clean-all/rebuild to no avail. I completely closed the solution, re-opened, and all was well again.
  • The Muffin Man
    The Muffin Man over 8 years
    #2 was the problem for me. The Debug profile had Optimize checked for some reason.