1

I'm investigating a memory leak issue from production and retrieved a memory dump. I'm trying to dump the values of the accumulated object, the I met WeakReference. Here's what I got in WinDBG:

0:000> !do 000000011a306510 
Name:        System.WeakReference
MethodTable: 000007feeb3f9230
EEClass:     000007feeadda218
Size:        24(0x18) bytes
File:        C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
Fields:
              MT    Field   Offset                 Type VT     Attr            Value Name
000007feeb3f4a00  400068d        8        System.IntPtr  1 instance         343620e0 m_handle
0:000> !do 343620e0 
<Note: this object has an invalid CLASS field>
Invalid object

We can find out that we cannot use the m_handle value as the object address. I've check the code of WeakReference and it's fully extern codes.

My question is, how can we inspect the value of it using WinDBG/SOS? Also, I'm writing ad-hoc analyzer for the problem with ClrMD, so how should I check the object references by the WeakReference object with it?

Jeffrey Zhao
  • 4,923
  • 4
  • 30
  • 52

2 Answers2

7

m_handle is an IntPtr which is a value type, so get the method table for IntPtrusing !name2ee *!System.IntPtr, then do

!dumpvc <method table of IntPtr> <value of m_handle>

This will give you the value the IntPtr points to. Since it points to an object, just dump that

!do <value of IntPtr>
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
2

Thanks Thomas for the answer.

Here's the code for getting the object address reference by an WeakReference object in ClrMD:

private static readonly ClrType WeakRefType = Heap.GetTypeByName("System.WeakReference");
private static readonly ClrInstanceField WeakRefHandleField = WeakRefType.GetFieldByName("m_handle");
private static readonly ClrType IntPtrType = Heap.GetTypeByName("System.IntPtr");
private static readonly ClrInstanceField IntPtrValueField = IntPtrType.GetFieldByName("m_value");

private static ulong GetWeakRefValue(ulong weakRefAddr)
{
    var handleAddr = (long)WeakRefHandleField.GetValue(weakRefAddr);
    var value = (ulong)IntPtrValueField.GetValue((ulong)handleAddr, true);

    return value;
}

Hope it helps.

Jeffrey Zhao
  • 4,923
  • 4
  • 30
  • 52