垃圾回收导致程序崩溃(GC.Collect crash)

jizhiguo 2009-10-21 09:40:29
垃圾回收导致程序崩溃(GC.Collect crash)

环境:
winxp(sp3),vs2005,.net 2.0,c#语言

程序:
通过串口通信(2个串口,分别使用2个厂商提供dll文件)
通过TCP网络通信
操作数据库
程序在运行过程中,会向串口发送多次命令,每个命令就是一串字节数组。

问题:
向串口发送一批命令后,程序在CLR执行垃圾回收时崩溃,直接弹系统错误对话框(就是发送错误报告的那个对话框),为了查找问题,我在每条串口命令执行之前都强制执行GC.Collect()回收垃圾,发现每次都是到固定那条串口数据才崩溃。

我用WinDbg工具分析程序崩溃时用DebugDiag捕捉到dump文件,具体分析结果如下,请哪位达人帮忙分析解答。谢谢!jizhiguo@netease.com

-------------WinDbg分析结果----------

Loading Dump File [D:\Program Files\DebugDiag\Logs\Misc\NSIISys.exe__PID__2456__Date__10_21_2009__Time_02_50_39PM__31__Manual Dump.dmp]
User Mini Dump File with Full Memory: Only application data is available

Comment: 'Dump created by Debug Diagnostic Tool'
Symbol search path is: SRV*d:\websymbols*http://msdl.microsoft.com/download/symbols;D:\jizg\NSIISys\bin\Debug;D:\Symbols
Executable search path is:
Windows XP Version 2600 (Service Pack 3) MP (2 procs) Free x86 compatible
Product: WinNt, suite: SingleUserTS Personal
Machine Name:
Debug session time: Wed Oct 21 14:50:39.000 2009 (GMT+8)
System Uptime: 0 days 3:33:01.279
Process Uptime: 0 days 0:01:24.000
................................................................
......................
Loading unloaded module list
.....
eax=015ca5ac ebx=013c3518 ecx=79308060 edx=0015b210 esi=01412b28 edi=015ca908
eip=7c92e514 esp=0013ecc0 ebp=0013ed54 iopl=0 nv up ei pl zr na pe nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000246
ntdll!KiFastSystemCallRet:
7c92e514 c3 ret
0:000> kbn
# ChildEBP RetAddr Args to Child
00 0013ecbc 77d19418 7b1d8ea8 09c75026 79e7a6e8 ntdll!KiFastSystemCallRet
*** WARNING: Unable to verify checksum for System.Windows.Forms.ni.dll
01 0013ed54 7b1d8997 00000000 00000004 00000000 user32!NtUserWaitMessage+0xc
02 0013edac 7b1d87e1 01466204 00000000 00000000 System_Windows_Forms_ni+0x208997
03 0013eddc 7b6ede2b 015b1034 01457990 000301c6 System_Windows_Forms_ni+0x2087e1
04 0013edf4 7b7225ab 09c75026 79e7a6e8 0013f0a8 System_Windows_Forms_ni+0x71de2b
05 0013ee80 7b7227c3 00e7bd99 0013f1f4 79edeff8 System_Windows_Forms_ni+0x7525ab
06 0013eed0 7b1e2d98 01442108 013dff40 013dfee0 System_Windows_Forms_ni+0x7527c3
07 0013eee8 7b88a746 014cd8d8 013dfee0 014cd8d8 System_Windows_Forms_ni+0x212d98
08 0013eefc 7b7467de 013dfee0 00000016 013dfee0 System_Windows_Forms_ni+0x8ba746
09 0013ef28 7b746e40 013dff40 014420ec 013dfee0 System_Windows_Forms_ni+0x7767de
0a 0013ef6c 7b746339 014cd8a4 013dfee0 0013ef94 System_Windows_Forms_ni+0x776e40
0b 0013ef7c 7ba34fcb 00000002 013dfee0 014cd8a4 System_Windows_Forms_ni+0x776339
0c 0013ef94 7b740cb6 00000002 0000000d 0000002a System_Windows_Forms_ni+0xa64fcb
0d 0013efc4 7b74a89d 09c75026 00000000 00000000 System_Windows_Forms_ni+0x770cb6
0e 0013f010 7b6f76f3 013f7af8 0000000f 000f002a System_Windows_Forms_ni+0x77a89d
0f 0013f094 7ba2a136 00000001 00100000 09c75026 System_Windows_Forms_ni+0x7276f3
10 0013f0f4 7b1d1dca 0015b210 0013f170 7b1f2051 System_Windows_Forms_ni+0xa5a136
11 0013f100 7b1f2051 09c75026 79e7a6e8 0013f338 System_Windows_Forms_ni+0x201dca
12 0013f170 7b1affc6 0013f1a8 013f7d24 0013f188 System_Windows_Forms_ni+0x222051
13 0013f180 7b1c86a0 0013f19c 7b1c8621 00000000 System_Windows_Forms_ni+0x1dffc6
0:000> .loadby sos mscorwks
0:000> !clrstack
OS Thread Id: 0xbb8 (0)
ESP EIP
0013eccc 7c92e514 [InlinedCallFrame: 0013eccc] System.Windows.Forms.UnsafeNativeMethods.WaitMessage()
0013ecc8 7b1d8ea8 System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32, Int32, Int32)
0013ed64 7b1d8997 System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext)
0013edb8 7b1d87e1 System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext)
0013ede8 7b6ede2b System.Windows.Forms.Application.RunDialog(System.Windows.Forms.Form)
0013edfc 7b7225ab System.Windows.Forms.Form.ShowDialog(System.Windows.Forms.IWin32Window)
0013ee88 7b7227c3 System.Windows.Forms.Form.ShowDialog()
0013ee8c 00e7bd99 NSIISys.FrmMain.自动发卡机ToolStripMenuItem1_Click(System.Object, System.EventArgs)
0013eedc 7b1e2d98 System.Windows.Forms.ToolStripItem.RaiseEvent(System.Object, System.EventArgs)
0013eef4 7b88a746 System.Windows.Forms.ToolStripMenuItem.OnClick(System.EventArgs)
0013ef04 7b7467de System.Windows.Forms.ToolStripItem.HandleClick(System.EventArgs)
0013ef30 7b746e40 System.Windows.Forms.ToolStripItem.HandleMouseUp(System.Windows.Forms.MouseEventArgs)
0013ef74 7b746339 System.Windows.Forms.ToolStripItem.FireEventInteractive(System.EventArgs, System.Windows.Forms.ToolStripItemEventType)
0013ef88 7ba34fcb System.Windows.Forms.ToolStripItem.FireEvent(System.EventArgs, System.Windows.Forms.ToolStripItemEventType)
0013efa0 7b740cb6 System.Windows.Forms.ToolStrip.OnMouseUp(System.Windows.Forms.MouseEventArgs)
0013efcc 7b74a89d System.Windows.Forms.ToolStripDropDown.OnMouseUp(System.Windows.Forms.MouseEventArgs)
0013f018 7b6f76f3 System.Windows.Forms.Control.WmMouseUp(System.Windows.Forms.Message ByRef, System.Windows.Forms.MouseButtons, Int32)
0013f0a4 7ba2a136 System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message ByRef)
0013f0a8 7b1d1dca [InlinedCallFrame: 0013f0a8]
0013f108 7b1f2051 System.Windows.Forms.ToolStrip.WndProc(System.Windows.Forms.Message ByRef)
0013f10c 7b1affc6 [InlinedCallFrame: 0013f10c]
0013f188 7b1c86a0 System.Windows.Forms.Control+ControlNativeWindow.OnMessage(System.Windows.Forms.Message ByRef)
0013f190 7b1c8621 System.Windows.Forms.Control+ControlNativeWindow.WndProc(System.Windows.Forms.Message ByRef)
0013f1a4 7b1c84fa System.Windows.Forms.NativeWindow.Callback(IntPtr, Int32, IntPtr, IntPtr)
0013f338 003b20a4 [NDirectMethodFrameStandalone: 0013f338] System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG ByRef)
0013f348 7b1d8d2e System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32, Int32, Int32)
0013f3e4 7b1d8997 System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext)
0013f438 7b1d87e1 System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext)
0013f468 7b195931 System.Windows.Forms.Application.Run(System.Windows.Forms.Form)
0013f47c 00e700ae NSIISys.Program.Main()
0013f69c 79e71b4c [GCFrame: 0013f69c]
...全文
266 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuanhuiqiao 2009-10-23
  • 打赏
  • 举报
回复
这些不足以找到问题,看看程序有没有泄露(leak),用perfmon里的参数观察一下,参考
jizhiguo 2009-10-23
  • 打赏
  • 举报
回复
我想,如果能保证在串口函数调用期间,不要执行垃圾回收,也应该可以解决这个问题。只是不知道如何做才能确保某个函数被调用期间,垃圾收集器暂停,不要动作。
yidichaxiang 2009-10-22
  • 打赏
  • 举报
回复
mark
trentliu 2009-10-22
  • 打赏
  • 举报
回复
你程序里没有try catch 么? 按照你这搞法,应该在debug运行时,能捕捉到
jizhiguo 2009-10-22
  • 打赏
  • 举报
回复
try catch的问题就不要猜测了,我已经在所有可能出错的地方都try了。
V68V6 2009-10-21
  • 打赏
  • 举报
回复
还真没遇到过
关注

友情顶贴。。。
jizhiguo 2009-10-21
  • 打赏
  • 举报
回复
我所谓的崩溃,就是程序出错,操作系统弹出一个对话框,问要不要发送错误报告。我通常点击“不发送”。
jizhiguo 2009-10-21
  • 打赏
  • 举报
回复
我确定是GC.Collect()引起的。

开始的时候,我没有在每条串口指令前调用GC.Collect(),而是由CLR负责垃圾回收,这样,程序崩溃就是不可预期的,有可能是几十条串口指令就崩溃,也可能几百条才崩溃。

后台为了定位问题,我才手动在每条串口指令前调用GC.Collect(),这样一来,崩溃就可准确重现了,一定是在执行完n条串口指令后,在执行n+1条串口指令前调用GC.Collect()时崩溃。注:n是常数,且这n条指令每次执行都是一样的指令。第n+1条也是每次执行都是一样的指令。

高手帮我分析分析,已经好几天了,找不到解决办法。
极地_雪狼 2009-10-21
  • 打赏
  • 举报
回复
你贴出的东西貌似没有问题啊,你说所的崩溃是什么现象啊?
V68V6 2009-10-21
  • 打赏
  • 举报
回复
up

你确定是GC引起的?
十八道胡同 2009-10-21
  • 打赏
  • 举报
回复
dump文件 我看不懂。。

110,529

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧