当第一次发生 OutOfMemoryException 时尝试使用 DebugDiag 进行转储时,我遇到了一个问题。所以我写了一个应用程序,我可以用它来创建内存不足的情况,并按照以下说明进行操作:
但我没有第一次转储,我只是获得第二次转储。当我查看来自 DebugDiag 的日志文件时,我得到以下信息:
[6/16/2013 9:54:04 PM] First chance exception - 0xe06d7363 caused by thread with System ID: 4628
[6/16/2013 9:54:04 PM] First chance exception - 0xe0434352 caused by thread with System ID: 4628
[6/16/2013 9:54:05 PM] Unable to determine CLR exception type
ExceptionObjHexAddr = 0x00000000`00000000
bInnerException = False
DumpObject Output = Invalid parameter 0x00000000`00000000
ChildEBP RetAddr Args to Child
002bdee4 6a44c93f e0434352 00000001 00000005 KERNELBASE!RaiseException+0x58
002bdf88 6a573b17 00000000 20b578f4 002be04c clr!RaiseTheExceptionInternalOnly+0x276
002bdfb8 6a5e5589 20b54734 002be090 00000000 clr!UnwindAndContinueRethrowHelperAfterCatch+0x83
002be058 003c0a3a 00000000 00000000 0233d174 clr!JIT_NewArr1+0x1af
... removed some rows, lots of data ...
OS Thread Id: 0x1214 (0)
Child SP IP Call Site
002bdfd4 7554c41f [Frame: 002bdfd4]
002be060 003c0a3a
... removed some rows, lots of data ...
Error requesting GC Heap data
Unable to determine bounds of gc heap
然后后来我得到这个:
[6/16/2013 9:54:05 PM] CLR Exception Type - ''
[6/16/2013 9:54:05 PM] First chance exception - 0xe0434352 caused by thread with System ID: 4628
[6/16/2013 9:54:05 PM] Unable to determine CLR exception type
最后我得到了
[6/16/2013 9:54:05 PM] CLR Exception Type - ''
[6/16/2013 9:54:05 PM] C:\Windows\Microsoft.NET\Framework\v4.0.30319\diasymreader.dll loaded at 0x615c0000
[6/16/2013 9:54:13 PM] Second chance exception - 0xe0434352 caused by thread with System ID: 4628
貌似可以获取异常对象的地址,为0,所以脚本调用DumpObject时找不到异常信息。
我阅读这些日志条目的方式是我从 malloc 或其他东西中获得原生的第一次机会异常,然后跟进 OutOfMemoryException 的 CLR 异常。我试图弄清楚第二个第一次机会异常是什么,我的代码如下所示:
private void OnGrowMemoryCommand(int growMemorySize)
{
try
{
_heldMemoryChunks.Add(new byte[growMemorySize * 1024 * 1024]);
}
catch (Exception)
{
throw;
}
TotalMemorySize += growMemorySize;
}
此代码是从 WPF 按钮上的命令触发的。因此,源自此代码的任何异常都应导致 TargetInvocationException,我认为这是第一次机会异常中的第二个。最后来自 throw 块的是第二次机会异常,其类型为 TargetInvocationException。
所以我开始查看第二次机会转储文件。我将它加载到 windbg 中,然后发出以下命令:
.symfix C:\symcache
.loadby sos clr
.reload
!pe
Exception object: 023caf9c
Exception type: System.Reflection.TargetInvocationException
Message: Exception has been thrown by the target of an invocation.
InnerException: System.OutOfMemoryException, Use !PrintException 023c9928 to see more.
我可以看到,我的上述假设得到了第二次机会异常是 TargetInvocationException 这一事实的支持,但是为什么 DebugDiag 不能获取 CLR 异常类型呢?对于健全性检查,我尝试进行实时调试会话。所以我启动应用程序并附加,然后发出这些命令。
.symfix C:\symcache
.loadby sos clr
.reload
!threads
Failed to request ThreadStore
!dumpheap
The garbage collector data structures are not in a valid state for traversal.
It is either in the "plan phase," where objects are being moved around, or
we are at the initialization or shutdown of the gc heap. Commands related to
displaying, finding or traversing objects as well as gc heap segments may not
work properly. !dumpheap and !verifyheap may incorrectly complain of heap
consistency errors.
Error requesting GC Heap data
Unable to build snapshot of the garbage collector state
这是完全软管。于是我开始研究这个问题。
此 url 表明它可能是多个 CLR 实例:
所以我发出这些命令:
.cordll
CLR DLL status: Loaded DLL C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscordacwks.dll
这对我来说很奇怪,我认为从 4.0 开始,mscorwks 就被 clr 抛弃了。mscordacwks 是 4.5 吗?
我发出了这个命令:
lmvm mscordacwks
但是 clr 已加载:
lmvm clr
start end module name
6a350000 6a9e2000 clr (pdb symbols) C:\symcache\clr.pdb\97FD69E1786F42F9A541C81D81AC96852\clr.pdb
Loaded symbol image file: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Image path: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Image name: clr.dll
Timestamp: Fri Mar 29 00:13:44 2013 (51553118)
CheckSum: 0069496E
ImageSize: 00692000
File version: 4.0.30319.18047
Product version: 4.0.30319.18047
File flags: 8 (Mask 3F) Private
File OS: 4 Unknown Win32
File type: 2.0 Dll
File date: 00000000.00000000
Translations: 0409.04b0
CompanyName: Microsoft Corporation
ProductName: Microsoft® .NET Framework
InternalName: clr.dll
OriginalFilename: clr.dll
ProductVersion: 4.0.30319.18047
FileVersion: 4.0.30319.18047 built by: FX45RTMGDR
PrivateBuild: DDBLD316
FileDescription: Microsoft .NET Runtime Common Language Runtime - WorkStation
LegalCopyright: © Microsoft Corporation. All rights reserved.
Comments: Flavor=Retail
所以我认为我没有加载多个 CLR。
因此,我假设导致我的实时调试问题的同一件事是导致我未能在第一次机会问题上触发。有任何想法吗?