|
在window平臺下,產生并進入臨界區EnterCriticalSection之后不釋放,用handlespy檢測,會產生一定數量的全局Event句柄泄露,甚是奇怪,開始以為EnterCriticalSection被重載了,幾經折騰,基本定位在EnterCriticalSection自身,列堆棧以觀之,發現EnterCriticalSection內部調用了NTCreateEvent函數, google一把,大概是說在多核處理器上,有時候會需要通過創建全局Event來干點啥CPU同步的事情,所以,在我的四核開發機器上,大概每20次就調用就會產生一次Event泄露。 知道原因好辦了,為CriticalSection增加引用計數器,沒人用刪掉,發了幾十萬筆,泄露為0,解決之。
工具:查泄露工具: HandleSpy.exe 開源工具,通過比較兩次快照就能得到泄露的代碼行,犀利。查句柄工具: SysinternalsSuite 包內的 handle.exe
順便鄙視一下windows8,任務管理器把handle計數器全搞沒了,對于我們這些搞開發的,真的非常不方便。
C:\Code\TechLab\MyTest\MyTest\Release>handle.exe -s -p kdfixgw.exe
Handle v3.51
Copyright (C) 1997-2013 Mark Russinovich
Sysinternals
Handle type summary:
ALPC Port : 4
Desktop : 1
Directory : 4
EtwRegistration : 71
Event : 99
File : 99
IoCompletion : 2
Key : 21
Mutant : 48
Section : 92
Semaphore : 104
Thread : 59
Timer : 3
TpWorkerFactory : 1
WaitCompletionPacket: 2
WindowStation : 2
Total handles: 612
|
|