Roslyn: C# Tuple is zeroed when accessed via collection

Created on 24 Jul 2019  路  3Comments  路  Source: dotnet/roslyn

Version Used: Visual Studio 15.9.04012.0. File -> New -> Project -> Console (DotNet Core)

Steps to Reproduce:

  1. Paste the following code:

    using System;
    using System.Collections.Generic;
    
    namespace CompilerBugZero
    {
       class Program
       {
           static void Main(string[] args)
           {
               var hdl = FindHandle();
               if (IntPtr.Zero == hdl)
               {
                   throw new InvalidOperationException();
               }
           }
    
           static IntPtr FindHandle()
           {
               var candidates = new List<(IntPtr hwnd, string title, string className)>();
               candidates.Add((new IntPtr(123), "title", "className"));
    
               if (candidates.Count == 1)
               {
                   // Breakpoint here
                   return candidates[0].hwnd;
               }
    
               throw new InvalidOperationException();
           }
       }
    }
    
    
  2. Set a breakpoint the line below // Breakpoint here

  3. See how everything is Zero when it shouldn't:
    image

    Note how the list still has the correct value but accessing [0] returns a zeroed tuple apparently.

Expected Behavior:
Debugger shows the correct values. Code works at runtime.

Actual Behavior:
Debugger doesn't show correct value. In fact in our real application I feel like we had the wrong behavior (ie IntPtr.Zero) at runtime. In the real application this happened in a PInvoke scenario. However, I couldn't reproduce this here. This might be a wrong positive, but worth considering when investigating this.

Workaround:

Change the code to

            if (candidates.Count == 1)
            {
                // Breakpoint here
-               return candidates[0].hwnd;
+               var t = candidates[0];
+               return t.hwnd;
            }
Area-External Interactive-Debugging Resolution-Duplicate

Most helpful comment

All 3 comments

I confirm this reproduces and it's likely bug in the EE. Very odd.

@0xd4d Thanks for tracking it down. Closing as a dup of dotnet/coreclr#25519.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nlwolf picture nlwolf  路  3Comments

AdamSpeight2008 picture AdamSpeight2008  路  3Comments

MadsTorgersen picture MadsTorgersen  路  3Comments

johndog picture johndog  路  3Comments

marler8997 picture marler8997  路  3Comments