Plug various handle+mem leaks on win32

Submitted by: Jan Kneschke <jan.kneschke@mysql.com>
This commit is contained in:
Doug MacEachern 2007-03-07 23:24:41 +00:00
parent 795db98647
commit 1affc51534
3 changed files with 27 additions and 5 deletions

View File

@ -1,3 +1,8 @@
2007-03-07 Doug MacEachern <dougm@hyperic.com>
* Plug various handle+mem leaks on win32
Submitted by: Jan Kneschke <jan.kneschke@mysql.com>
2007-02-24 Doug MacEachern <dougm@hyperic.com> 2007-02-24 Doug MacEachern <dougm@hyperic.com>
* Make Java objects returned by Sigar class Serializable * Make Java objects returned by Sigar class Serializable

View File

@ -198,6 +198,7 @@ static unsigned char CPUCount(unsigned char *LogicalNum,
if (dwProcessAffinity != dwSystemAffinity) { if (dwProcessAffinity != dwSystemAffinity) {
StatusFlag = HT_CANNOT_DETECT; StatusFlag = HT_CANNOT_DETECT;
// *PhysicalNum = (unsigned char)-1; // *PhysicalNum = (unsigned char)-1;
CloseHandle(hCurrentProcessHandle);
return StatusFlag; return StatusFlag;
} }
@ -244,6 +245,7 @@ static unsigned char CPUCount(unsigned char *LogicalNum,
StatusFlag = HT_SUPPORTED_NOT_ENABLED; StatusFlag = HT_SUPPORTED_NOT_ENABLED;
} }
} }
CloseHandle(hCurrentProcessHandle);
} }
} }
else { else {
@ -299,11 +301,13 @@ int sigar_cpu_info_get(sigar_t *sigar, sigar_cpu_info_t *info)
//just lookup the first id, then assume all cpus are the same. //just lookup the first id, then assume all cpus are the same.
rc = RegEnumKey(key, 0, id, sizeof(id)); rc = RegEnumKey(key, 0, id, sizeof(id));
if (rc != ERROR_SUCCESS) { if (rc != ERROR_SUCCESS) {
RegCloseKey(key);
return rc; return rc;
} }
rc = RegOpenKey(key, id, &cpu); rc = RegOpenKey(key, id, &cpu);
if (rc != ERROR_SUCCESS) { if (rc != ERROR_SUCCESS) {
RegCloseKey(key);
return rc; return rc;
} }
@ -343,6 +347,8 @@ int sigar_cpu_info_get(sigar_t *sigar, sigar_cpu_info_t *info)
} }
info->cache_size = -1; //XXX info->cache_size = -1; //XXX
RegCloseKey(key);
RegCloseKey(cpu);
return SIGAR_OK; return SIGAR_OK;
} }

View File

@ -1108,6 +1108,7 @@ SIGAR_DECLARE(int) sigar_proc_time_get(sigar_t *sigar, sigar_pid_t pid,
{ {
HANDLE proc = open_process(pid); HANDLE proc = open_process(pid);
FILETIME start_time, exit_time, system_time, user_time; FILETIME start_time, exit_time, system_time, user_time;
int status = ERROR_SUCCESS;
if (!proc) { if (!proc) {
return GetLastError(); return GetLastError();
@ -1117,11 +1118,15 @@ SIGAR_DECLARE(int) sigar_proc_time_get(sigar_t *sigar, sigar_pid_t pid,
&start_time, &exit_time, &start_time, &exit_time,
&system_time, &user_time)) &system_time, &user_time))
{ {
return GetLastError(); status = GetLastError();
} }
CloseHandle(proc); CloseHandle(proc);
if (status != ERROR_SUCCESS) {
return status;
}
if (start_time.dwHighDateTime) { if (start_time.dwHighDateTime) {
proctime->start_time = FileTimeToTime(&start_time) / 1000; proctime->start_time = FileTimeToTime(&start_time) / 1000;
} }
@ -1347,6 +1352,7 @@ static int sigar_remote_proc_env_get(sigar_t *sigar, sigar_pid_t pid,
char *value; char *value;
DWORD rv, thrid, bytes, datalen=0, size; DWORD rv, thrid, bytes, datalen=0, size;
LPVOID addr; LPVOID addr;
int status;
if (!(kdll = GetModuleHandle("msvcrt.dll"))) { if (!(kdll = GetModuleHandle("msvcrt.dll"))) {
return GetLastError(); return GetLastError();
@ -1364,6 +1370,8 @@ static int sigar_remote_proc_env_get(sigar_t *sigar, sigar_pid_t pid,
return GetLastError(); return GetLastError();
} }
/* FIXME: close the kdll handles */
if (!(proc = OpenProcess(MAXIMUM_ALLOWED, 0, (DWORD)pid))) { if (!(proc = OpenProcess(MAXIMUM_ALLOWED, 0, (DWORD)pid))) {
return GetLastError(); return GetLastError();
} }
@ -1435,15 +1443,16 @@ static int sigar_remote_proc_env_get(sigar_t *sigar, sigar_pid_t pid,
key, strlen(key), key, strlen(key),
value, bytes-1); value, bytes-1);
HeapFree(GetProcessHeap(), 0, value); status = SIGAR_OK;
} }
else { else {
CloseHandle(proc); status = GetLastError();
return GetLastError();
} }
HeapFree(GetProcessHeap(), 0, value);
CloseHandle(proc); CloseHandle(proc);
return SIGAR_OK;
return status;
} }
SIGAR_DECLARE(int) sigar_proc_env_get(sigar_t *sigar, sigar_pid_t pid, SIGAR_DECLARE(int) sigar_proc_env_get(sigar_t *sigar, sigar_pid_t pid,
@ -1533,6 +1542,8 @@ SIGAR_DECLARE(int) sigar_proc_exe_get(sigar_t *sigar, sigar_pid_t pid,
procexe->name[0] = toupper(procexe->name[0]); procexe->name[0] = toupper(procexe->name[0]);
} }
CloseHandle(proc);
return status; return status;
} }