Skip to content

Invalid backing object for unused nullable static value types #41420

Open
@isadorasophia

Description

@isadorasophia

It seems that the CLR is not correctly handling initialization of nullable static variables. The repro steps below should make the problem clearer.

Repro steps:

  1. Start debugging the following app, either on x86 or x64, for .NET version 3.1.401 or 5.0.100-preview.8.20362.3:
class NullableTest
{
    static int? i;

    public static void Main()
    {
        System.Diagnostics.Debugger.Break();

        if (i.HasValue)
        {
            System.Console.WriteLine("Value is {0}!", i.Value);
        }
    }
}
  1. Stop at breakpoint;
  2. Evaluate i at the Watch Window. The following error message will show up: "The debugger is unable to evaluate this expression."
  3. Stop at breakpoint and evaluate i = 2 at the Immediate Window;
  4. Evaluate i again, it will now have a value of 2;
  5. Step over twice;
  6. The condition will be skipped and i value is now null.

Regression?

This is a regression when upgrading from 3.1.302 -> 3.1.401. It has also been observed on 5.0.100-preview.3.20216.6 -> 5.0.100-preview.8.20362.3.

Other information

  • The CLR is directly returning a null slot for the variable until that value is actually used, which results in the error message shown at step 3. I verified that ICorDebugReferenceValue::GetValue returns 0 at that point.
  • After line 9 is executed, ICorDebugReferenceValue::GetValue returns a valid address, which also overwrites any data that was there before (from step 4).

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions