Search code examples
c#garbage-collectionfinalization

How to list all object in GC finalization list?


I have crash in my program, it is a visualizer for VS, so, it is very hard to debug it, i have tried to make dump and use WinDbg to study it, but it unsuccessful.

So, now i try to put my hands on that list programmatically, but i don't know how. Thanks.


Solution

  • I do not think that there is a way to get at the finalization queue via .NET's managed Framework Class Library (FCL). I suspect that if you want to do this programmatically instead of debugging with WinDbg, you (just like WinDbg and similar tools) will need to use the CLR's unmanaged debugging & profiling APIs towards that end.

    Take a look at the ICORDebugGCReferenceEnum COM interface. You can retrieve on object of that type via ICorDebugProcess5::EnumerateGCReferences:

    "Provides an enumerator for objects that will be garbage-collected."

    "The COR_GC_REFERENCE objects in the collection populated by [the ICorDebugGCReferenceEnum::Next method] represent three kinds of objects:

    • Objects from all managed stacks. This includes live references in managed code as well as objects created by the common language runtime.

    • Objects from the handle table. This includes strong references (HNDTYPE_STRONG and HNDTYPE_REFCOUNT) and static variables in a module.

    • Objects from the finalizer queue. The finalizer queue roots objects until the finalizer has run."

    (Hyperlinks and emphasis added by me.)

    Each object returned by the enumerator has a field type. You might want to filter for objects where that field matches the value CorGCReferenceType.CorReferenceFinalizer.