invalidGCHandleCookie MDA

Note

This article is specific to .NET Framework. It doesn't apply to newer implementations of .NET, including .NET 6 and later versions.

The invalidGCHandleCookie managed debugging assistant (MDA) is activated when a conversion from an invalid IntPtr cookie to a GCHandle is attempted.

Symptoms

Undefined behavior such as access violations and memory corruption while attempting to use or retrieve a GCHandle from an IntPtr.

Cause

The cookie is probably invalid because it was not originally created from a GCHandle, represents a GCHandle that has already been freed, is a cookie to a GCHandle in a different application domain, or was marshalled to native code as a GCHandle but passed back into the CLR as an IntPtr, where a cast was attempted.

Resolution

Specify a valid IntPtr cookie for the GCHandle.

Effect on the Runtime

When this MDA is enabled, the debugger is no longer able to trace the roots back to their objects because the cookie values passed back are different from the ones returned when the MDA is not enabled.

Output

The invalid IntPtr cookie value is reported.

Configuration

<mdaConfig>
  <assistants>
    <invalidGCHandleCookie />
  </assistants>
</mdaConfig>

See also