如何使用WinDBG/SOS和ClrMD检查WeakReference值?

如何使用WinDBG/SOS和ClrMD检查WeakReference值?,windbg,weak-references,sos,clrmd,Windbg,Weak References,Sos,Clrmd,我正在调查生产中的内存泄漏问题,并检索到内存转储。我试图转储累积对象的值,即我遇到的WeakReference。以下是我在WinDBG中得到的信息: 0:000> !do 000000011a306510 Name: System.WeakReference MethodTable: 000007feeb3f9230 EEClass: 000007feeadda218 Size: 24(0x18) bytes File: C:\Windo

我正在调查生产中的内存泄漏问题,并检索到内存转储。我试图转储累积对象的值,即我遇到的
WeakReference
。以下是我在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
0:000>!请勿使用0000000 11A306510
名称:System.WeakReference
方法表:000007feeb3f9230
EEClass:000007 feeAdd218
大小:24(0x18)字节
文件:C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0.0.0\uu_b77a5c561934e089\mscorlib.dll
领域:
MT字段偏移类型VT属性值名称
000007feeb3f4a00 400068d 8 System.IntPtr 1实例343620e0 m_手柄
0:000> !做343620e0吗
无效对象
我们可以发现不能使用
m_handle
值作为对象地址。我已经检查了
WeakReference
的代码,它完全是
extern
代码


我的问题是,我们如何使用WinDBG/SOS检查it的价值?另外,我正在为ClrMD的问题编写一个特设分析器,那么我应该如何通过
WeakReference
对象检查对象引用呢?

m\u handle
是一个
IntPtr
值类型,所以使用
获取
IntPtr
的方法表!名称2ee*!System.IntPtr
,然后执行

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

m_handle
是一个
IntPtr
值类型,因此使用
获取
IntPtr
的方法表!名称2ee*!System.IntPtr
,然后执行

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

谢谢托马斯的回答

下面是通过ClrMD中的
WeakReference
对象获取对象地址引用的代码:

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;
}

希望有帮助。

谢谢托马斯的回答

下面是通过ClrMD中的
WeakReference
对象获取对象地址引用的代码:

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;
}
希望能有帮助