Repository: dotnet/runtime
File: src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Legacy/SOSDacImpl.cs (C#)

Code before:
        {
            Dictionary<ulong, uint> expectedElements = elements.ToDictionary(tuple => tuple.Address.ToClrDataAddress(_target).Value, tuple => tuple.Index);
            expectedElements.Add(default, 0);
            void* tokenDebug = GCHandle.ToIntPtr(GCHandle.Alloc(expectedElements)).ToPointer();
            delegate* unmanaged[Stdcall]<uint, ulong, void*, void> callbackDebugPtr = &TraverseModuleMapCallback;

            int hrLocal = _legacyImpl.TraverseModuleMap(mmt, moduleAddr, callbackDebugPtr, tokenDebug);
            Debug.Assert(hrLocal == hr, $"cDAC: {hr:x}, DAC: {hrLocal:x}");
            Debug.Assert(expectedElements[default] == elements.Count(), $"cDAC: {elements.Count()} elements, DAC: {expectedElements[default]} elements");
            GCHandle.FromIntPtr((nint)tokenDebug).Free();
        }
#endif
        return hr;
    }
#if DEBUG
    [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]
    private static Interop.BOOL TraverseRCWCleanupListCallback(ulong rcwAddr, ulong ctx, ulong staThread, Interop.BOOL isFreeThreaded, void* expectedElements)
    {
        var expectedElementsDict = (Dictionary<ulong, ulong>)GCHandle.FromIntPtr((nint)expectedElements).Target!;
        if (expectedElementsDict.TryGetValue(rcwAddr, out ulong expectedCtx) && expectedCtx == ctx)
        {
            expectedElementsDict[default]++; // Increment the count for verification
        }
        else
        {
            Debug.Assert(false, $"Unexpected RCW address {rcwAddr:x} or context {ctx:x}");
        }
        return Interop.BOOL.TRUE;
    }
#endif
    int ISOSDacInterface.TraverseRCWCleanupList(ClrDataAddress cleanupListPtr, delegate* unmanaged[Stdcall]<ulong, ulong, ulong, Interop.BOOL, void*, Interop.BOOL> pCallback, void* token)
    {
        int hr = HResults.S_OK;
        IEnumerable<Contracts.RCWCleanupInfo> cleanupInfos = Enumerable.Empty<Contracts.RCWCleanupInfo>();
        try
        {
            if (pCallback is null)
                throw new ArgumentException();

            Contracts.IBuiltInCOM contract = _target.Contracts.BuiltInCOM;
            TargetPointer listPtr = cleanupListPtr.ToTargetPointer(_target);

            cleanupInfos = contract.GetRCWCleanupList(listPtr);
            foreach (Contracts.RCWCleanupInfo info in cleanupInfos)
            {
                pCallback(
                    info.RCW.ToClrDataAddress(_target).Value,
                    info.Context.ToClrDataAddress(_target).Value,
                    info.STAThread.ToClrDataAddress(_target).Value,
                    info.IsFreeThreaded ? Interop.BOOL.TRUE : Interop.BOOL.FALSE,
                    token);

Review comment (bug):
```suggestion
            Debug.Fail($"Unexpected RCW address {rcwAddr:x} or context {ctx:x}");
```

Code after:
        {
            Dictionary<ulong, uint> expectedElements = elements.ToDictionary(tuple => tuple.Address.ToClrDataAddress(_target).Value, tuple => tuple.Index);
            expectedElements.Add(default, 0);
            void* tokenDebug = GCHandle.ToIntPtr(GCHandle.Alloc(expectedElements)).ToPointer();
            delegate* unmanaged[Stdcall]<uint, ulong, void*, void> callbackDebugPtr = &TraverseModuleMapCallback;

            int hrLocal = _legacyImpl.TraverseModuleMap(mmt, moduleAddr, callbackDebugPtr, tokenDebug);
            Debug.Assert(hrLocal == hr, $"cDAC: {hr:x}, DAC: {hrLocal:x}");
            Debug.Assert(expectedElements[default] == elements.Count(), $"cDAC: {elements.Count()} elements, DAC: {expectedElements[default]} elements");
            GCHandle.FromIntPtr((nint)tokenDebug).Free();
        }
#endif
        return hr;
    }
#if DEBUG
    [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]
    private static Interop.BOOL TraverseRCWCleanupListCallback(ulong rcwAddr, ulong ctx, ulong staThread, Interop.BOOL isFreeThreaded, void* expectedElements)
    {
        var expectedElementsDict = (Dictionary<ulong, ulong>)GCHandle.FromIntPtr((nint)expectedElements).Target!;
        if (expectedElementsDict.TryGetValue(rcwAddr, out ulong expectedCtx) && expectedCtx == ctx)
        {
            expectedElementsDict[default]++; // Increment the count for verification
        }
        else
        {
            Debug.Fail($"Unexpected RCW address {rcwAddr:x} or context {ctx:x}");
        }
        return Interop.BOOL.TRUE;
    }
#endif
    int ISOSDacInterface.TraverseRCWCleanupList(ClrDataAddress cleanupListPtr, delegate* unmanaged[Stdcall]<ulong, ulong, ulong, Interop.BOOL, void*, Interop.BOOL> pCallback, void* token)
    {
        int hr = HResults.S_OK;
        IEnumerable<Contracts.RCWCleanupInfo> cleanupInfos = Enumerable.Empty<Contracts.RCWCleanupInfo>();
        try
        {
            if (pCallback is null)
                throw new ArgumentException();

            Contracts.IBuiltInCOM contract = _target.Contracts.BuiltInCOM;
            TargetPointer listPtr = cleanupListPtr.ToTargetPointer(_target);

            cleanupInfos = contract.GetRCWCleanupList(listPtr);
            foreach (Contracts.RCWCleanupInfo info in cleanupInfos)
            {
                pCallback(
                    info.RCW.ToClrDataAddress(_target).Value,
                    info.Context.ToClrDataAddress(_target).Value,
                    info.STAThread.ToClrDataAddress(_target).Value,
                    info.IsFreeThreaded ? Interop.BOOL.TRUE : Interop.BOOL.FALSE,
                    token);