[c++] is het mogelijk handles te printen ?

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Hallo allemaal,

Ik ben voor mijn afstudeer stage met een c++ applicatie bezig... deze applicatie
zal het lezen en schrijven op een National Instruments kaart realiseren.

Helaas heb ik last van handle leaks, als ik met process explorer het process bekijk zie ik ook steeds nieuwe handles aangemaakt worden.

Normaal gesproken zou debuggen gemakkelijk zijn om opzoek te gaan naar deze leak, echter is het probleem tijdsgebonden, bij een Sleep(500) of hoger bijvoorbeeld werkt het programma naar behoren.

Ik ben opzoek gegaan naar een manier om van het process zelf het aantal gelekte handles tijdens het draaien van het programma te kunnen loggen, om er zo achter te komen door middel van het aantal gelekte handles waar het probleem zich voordoet.

wat ik heb gevonden is:


code: filename
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
bool ListThreads::GetProcessList()
{
  HANDLE hProcessSnap;
  HANDLE hProcess;
  PROCESSENTRY32 pe32;
  DWORD dwPriorityClass;

  // Take a snapshot of all processes in the system.
  hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPALL,NULL );
  if( hProcessSnap == INVALID_HANDLE_VALUE )
  {
    printError( TEXT("CreateToolhelp32Snapshot (of processes)") );
    return( false );
  }

  // Set the size of the structure before using it.
  pe32.dwSize = sizeof( PROCESSENTRY32 );

  // Retrieve information about the first process,
  // and exit if unsuccessful
  if( !Process32First( hProcessSnap, &pe32 ) )
  {
    printError( TEXT("Process32First") ); // show cause of failure
    CloseHandle( hProcessSnap );          // clean the snapshot object
    return( false );
  }

  while( Process32Next( hProcessSnap, &pe32 )){

      char *tekst = "Core_debug.exe";

      printf( TEXT("\nExecutable name:  %s"), pe32.szExeFile );
      if ( 0 == strcmp(pe32.szExeFile,tekst)){
          printf( TEXT("\n\n=====================================================" ));
            printf( TEXT("\nPROCESS NAME:  %s"), pe32.szExeFile );
            printf( TEXT("\n-------------------------------------------------------" ));

            // Retrieve the priority class.
            dwPriorityClass = 0;
            hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );
            if( hProcess == NULL )
              printError( TEXT("OpenProcess") );
            else
            {
              dwPriorityClass = GetPriorityClass( hProcess );
              if( !dwPriorityClass )
                printError( TEXT("GetPriorityClass") );
              CloseHandle( hProcess );
            }

            _tprintf( TEXT("\n  Process ID        = 0x%08X"), pe32.th32ProcessID );
            _tprintf( TEXT("\n  Thread count      = %d"),   pe32.cntThreads );
            _tprintf( TEXT("\n  Parent process ID = 0x%08X"), pe32.th32ParentProcessID );
            _tprintf( TEXT("\n  Priority base     = %d"), pe32.pcPriClassBase );
            if( dwPriorityClass )
              _tprintf( TEXT("\n  Priority class    = %d"), dwPriorityClass );

            CloseHandle( hProcess );

            // List the modules and threads associated with this process
            ListProcessHeap(pe32.th32ProcessID);
            ListProcessModules( pe32.th32ProcessID );
            ListProcessThreads( pe32.th32ProcessID );

      }
  }


Nou vroeg ik me af of iemand hier misschien weet of het mogelijk is om het aantal handles, zoals process explorer ze aangeeft, op een zelfde manier te printen als hierboven.

Hopelijk kan iemand me verder helpen, heel erg bedankt alvast.

mvg,
Robin

Acties:
  • 0 Henk 'm!

  • Gimmeabrake
  • Registratie: December 2008
  • Laatst online: 21-09 16:52
offtopic:
Ik weet niks van C++, maar ik raad je wel aan de code tussen code tags te zetten. Dat zullen de mensen die je wel kunnen helpen zeer op prijs stellen.

Acties:
  • 0 Henk 'm!

  • Mijzelf
  • Registratie: September 2004
  • Niet online
GetProcessHandleCount()

Acties:
  • 0 Henk 'm!

  • Soultaker
  • Registratie: September 2000
  • Laatst online: 22:43
Probeer Handle uit de SysInternals suite eens? Daarmee zou je in ieder geval moeten kunnen zien wat voor soort handles je open hebt.

Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
thnx! morgen gelijk proberen _/-\o_

wist nog niet van deze functie, op handle issues zoeken op google levert niet echt de gewenste resultaten :/


soultaker ook bedankt voor je reactie, ik weet al dat het om synchronisatie problemen gaat en helaas is een externe tool, terwijl ik een tijdsgebonden issue moet oplossen ook niet echt een succes. maar iig bedankt

ik denk dat
code:
1
2
3
4
BOOL WINAPI GetProcessHandleCount(
  __in     HANDLE hProcess,
  __inout  PDWORD pdwHandleCount
);


het al wel gaat oplossen! top _/-\o_

Acties:
  • 0 Henk 'm!

  • MSalters
  • Registratie: Juni 2001
  • Laatst online: 13-09 00:05
Handle leaks voorkom je overigens op dezelfde manier als memory leaks: RAII.

Man hopes. Genius creates. Ralph Waldo Emerson
Never worry about theory as long as the machinery does what it's supposed to do. R. A. Heinlein

Pagina: 1