在转储文件中查找NullReferenceException的源代码

时间:2014-08-13 14:34:25

标签: .net nullreferenceexception windbg sos

转储文件中存在多个System.NullReferenceExceptions。反正知道哪个对象为null以及导致此引用的调用堆栈是什么? 以下是我在转储文件中看到的内容。

0:000> !DumpHeap -type NullReferenceException
         Address               MT     Size
000000008096da70 000007fcb5482048      160     
00000000809aee40 000007fcb5482048      160     
00000000809fb248 000007fcb5482048      160     
0000000080a55ce8 000007fcb5482048      160     
0000000080a93098 000007fcb5482048      160     
0000000080af1278 000007fcb5482048      160     
00000000817cf458 000007fcb5482048      160     
000000018044ec08 000007fcb5482048      160     
00000003806e75b8 000007fcb5482048      160     
000000038072bdc0 000007fcb5482048      160     
000000038077a7e8 000007fcb5482048      160     
00000003807e4920 000007fcb5482048      160     
0000000380828418 000007fcb5482048      160     
0000000380874f78 000007fcb5482048      160     

Statistics:
              MT    Count    TotalSize Class Name
000007fcb5482048       14         2240 System.NullReferenceException
Total 14 objects
Fragmented blocks larger than 0.5 MB:
            Addr     Size      Followed by
00000001805e75f0    1.1MB 000000018070d328 System.Threading.ThreadStart
0000000281047828    4.8MB 0000000281519cd0 System.Threading.ThreadStart
0000000281538100    4.9MB 0000000281a15730 System.Threading.ThreadStart

0:000> !do 000000008096da70 
Name:        System.NullReferenceException
MethodTable: 000007fcb5482048
EEClass:     000007fcb4e50f30
Size:        160(0xa0) 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
000007fcb5476508  4000002        8        System.String  0 instance 0000000000000000 _className
000007fcb547e880  4000003       10 ...ection.MethodBase  0 instance 0000000000000000 _exceptionMethod
000007fcb5476508  4000004       18        System.String  0 instance 0000000000000000 _exceptionMethodString
000007fcb5476508  4000005       20        System.String  0 instance 00000001801d4df8 _message
000007fcb547e238  4000006       28 ...tions.IDictionary  0 instance 0000000000000000 _data
000007fcb5476738  4000007       30     System.Exception  0 instance 0000000000000000 _innerException
000007fcb5476508  4000008       38        System.String  0 instance 0000000000000000 _helpURL
000007fcb5476ae8  4000009       40        System.Object  0 instance 000000008096dc00 _stackTrace
000007fcb5476ae8  400000a       48        System.Object  0 instance 0000000000000000 _watsonBuckets
000007fcb5476508  400000b       50        System.String  0 instance 0000000000000000 _stackTraceString
000007fcb5476508  400000c       58        System.String  0 instance 0000000000000000 _remoteStackTraceString
000007fcb54792b8  400000d       88         System.Int32  1 instance                0 _remoteStackIndex
000007fcb5476ae8  400000e       60        System.Object  0 instance 0000000000000000 _dynamicMethods
000007fcb54792b8  400000f       8c         System.Int32  1 instance      -2147467261 _HResult
000007fcb5476508  4000010       68        System.String  0 instance 0000000000000000 _source
000007fcb547a338  4000011       78        System.IntPtr  1 instance                0 _xptrs
000007fcb54792b8  4000012       90         System.Int32  1 instance       -532462766 _xcode
000007fcb542cdd8  4000013       80       System.UIntPtr  1 instance      7fc56d78df9 _ipForWatsonBuckets
000007fcb5499eb8  4000014       70 ...ializationManager  0 instance 000000008096dbc8 _safeSerializationManager
000007fcb5476ae8  4000001        0        System.Object  0   shared           static s_EDILock
                                 >> Domain:Value  0000000002b83fd0:NotInit  00000000281b5fd0:NotInit  0000000022890fd0:NotInit  000000002877efd0:NotInit  0000000021104fd0:NotInit  0000000025c65fd0:NotInit  000000002c4c5fd0:NotInit  000000002c6b8fd0:NotInit  000000002c644fd0:NotInit  000000002ce33fd0:NotInit  000000002cbccfd0:NotInit  <<
0:000> !do 00000001801d4df8 
Name:        System.String
MethodTable: 000007fcb5476508
EEClass:     000007fcb4d93750
Size:        132(0x84) bytes
File:        C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
String:      Object reference not set to an instance of an object.
Fields:
              MT    Field   Offset                 Type VT     Attr            Value Name
000007fcb54792b8  40000aa        8         System.Int32  1 instance               53 m_stringLength
000007fcb54777f0  40000ab        c          System.Char  1 instance               4f m_firstChar
000007fcb5476508  40000ac       18        System.String  0   shared           static Empty
                                 >> Domain:Value  0000000002b83fd0:NotInit  00000000281b5fd0:NotInit  0000000022890fd0:NotInit  000000002877efd0:NotInit  0000000021104fd0:NotInit  0000000025c65fd0:NotInit  000000002c4c5fd0:NotInit  000000002c6b8fd0:NotInit  000000002c644fd0:NotInit  000000002ce33fd0:NotInit  000000002cbccfd0:NotInit  <<
0:000> !GCRoot 000000008096da70 
Found 0 unique roots (run '!GCRoot -all' to see all roots).

1 个答案:

答案 0 :(得分:1)

你最好的办法是查看转储文件中的堆栈跟踪,这应该会给你一个关于代码在Null引用异常中爆炸的提示。

要使用WinDbg和SOS获取堆栈,请在转储文件中使用以下命令:

!threads

这应该显示一个线程列表。如果查看每个线程的右侧,您可能会看到(System.NullReferenceException)。您不必使用此命令,但只是为了证明您处于正确的轨道上,这很方便。以下是非常重要的命令:

!pe

这意味着打印例外。它应该为您显示NullReferenceException的详细信息,包括堆栈跟踪,这是关键。希望堆栈跟踪中有足够的细节来帮助您找到代码出错的大概位置。

编辑:

只是因为堆中存在Null引用异常,并不意味着发生了异常。例如,搜索System.OutOfMemoryException`,您也会在堆中看到其中一个。

基本上,这些对象是在堆上创建的,以便为使用它们做好准备。告诉标志是这样,在!threads的输出中,您没有看到列出的任何Null引用异常,这意味着没有发生。