2009-08-19 3 views
3

WinDbg를 사용하여 핸들 할당에 이르는 호출 스택을 kwown 할 수 있는지 궁금합니다.핸들이 할당 된 위치는 어디입니까?

:

HANDLE h = CreateEvent(NULL, FALSE, FALSE, NULL); 

:

#include <windows.h> 
#include <conio.h> 
#include <iostream> 

using namespace std; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    cout << "Press ENTER to leak handles." << endl; 

    _getch(); 

    cout << "Leaking handles" << endl; 

    for (int i = 0; i < 100; ++i) 
    { 
     HANDLE h = CreateEvent(NULL, FALSE, FALSE, NULL); 
     if (h != NULL) 
     { 
      cout << "."; 
     } 
    } 

    cout << "Handles leaked. Press ENTER to exit." << endl; 

    _getch(); 

    return 0; 
} 

WinDbg는 그것을 위로이 샘플을 제작하고, 소성 후에는 선 위의 샘플에서, 핸들을 할당 호출 스택을 얻을 수있다 !handle 명령으로 진행하고 있지만 지금까지 진행하지 않았습니다.

이것은 누출 분석을 처리하는 데 적절합니다. 나는 !htrace -enable!htrace -diff을 알고 있지만, 다른 사용법 시나리오입니다 (어떤 조합 방법이나 다른 사용법 벡터가 없으면 정보를 제공하십시오).

답변

4

해결책이 될 것 같다 무엇을 찾았

  1. 하여 프로그램의 핸들과 !htrace <handle>와 분석을위한 피크를 확인 프로그램을 실행 !htrace -enable
  2. 를 사용하여 추적을 활성화하고 핸들 누수 기다립니다
 
0:001> !htrace -enable 
Handle tracing enabled. 
Handle tracing information snapshot successfully taken. 
0:001> g 
0:001> !handle 
... 

Handle 7d8 
    Type   Event 
... 
111 Handles 
Type   Count 
Event   103 
File   3 
Port   1 
Directory  2 
WindowStation 1 
KeyedEvent  1 
0:001> !htrace 7d8 
-------------------------------------- 
Handle = 0x000007d8 - OPEN 
Thread ID = 0x00000fc4, Process ID = 0x000017a8 

0x0040106d: TestMemHandleLeak!wmain+0x0000006d 
0x0040151b: TestMemHandleLeak!__tmainCRTStartup+0x0000010f 
0x7c817077: kernel32!BaseProcessStart+0x00000023 

-------------------------------------- 
Parsed 0x64 stack traces. 
Dumped 0x1 stack traces. 

그리고는 요지로 그에서 코드의 라인을 얻을 수 있습니다 나는 그랬다 :

 
0:001> ln TestMemHandleLeak!wmain+0x0000006d 
f:\temp\windowsapplication3\testmemhandleleak\testmemhandleleak.cpp(22)