Share via


GcManagedToUnmanaged 

The GcManagedToUnmanaged managed debugging assistant (MDA) causes a garbage collection whenever a thread transitions from managed to unmanaged code.

Symptoms

An unmanaged user component throws an access violation when trying to use a managed object that had been exposed to COM. The COM object appears to have been released. The access violation is nondeterministic.

Cause

If an unmanaged component is not reference counting a managed COM object correctly, then the runtime could collect a managed object exposed to COM when the unmanaged component still holds a reference to the object. The runtime calls Release during garbage collections, so if the user component uses the object before the garbage collection occurs, then it will not yet have been collected. This is the source of the nondeterminism.

Resolution

Enabling this assistant reduces the time between when the object is eligible for collection and Release is called, helping to track down which unmanaged component first tries to access the collected object.

Effect on the Runtime

Causes a garbage collection whenever a thread transitions from managed to unmanaged code.

Output

This MDA produces no output.

Configuration

<mdaConfig>
  <assistants>
    <gcManagedToUnmanaged/>
  </assistants>
</mdaConfig>

See Also

Reference

GcUnmanagedToManaged
MarshalAsAttribute

Concepts

Diagnosing Errors with Managed Debugging Assistants
Interop Marshaling Overview

Other Resources

Interoperability