- handle leak with RegisterWaitForSingleObject
- Posted by stef on October 19th, 2005
Hello guys,
I've "found" the pretty nice "RegisterWaitForSingleObject" to notify
a child death.
But, with the example below, I don't know why and where, I noticed I
lost some significant handle
after all children stops !?
I think I'm not using the function correctly but where ?
Could you help me please...
note :
the "child.exe" is a simple puts("hello"); while(1);
#include <windows.h>
BOOL (WINAPI *_RegisterWaitForSingleObject)(PHANDLE, HANDLE, PVOID,
PVOID, ULONG, ULONG);
BOOL (WINAPI *_UnregisterWait)(HANDLE);
typedef struct
{
DWORD ProcessId;
HANDLE hProcess;
HANDLE hThread;
HANDLE hNew;
} S_PI;
/**
*
*/
main()
{
HINSTANCE h;
int i;
STARTUPINFO si;
PROCESS_INFORMATION pi;
S_PI *pInfo;
BOOL b;
h = LoadLibrary("kernel32.dll");
_RegisterWaitForSingleObject = GetProcAddress(h,
"RegisterWaitForSingleObject");
_UnregisterWait = GetProcAddress(h, "UnregisterWait");
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb= sizeof(STARTUPINFO);
si.dwFlags|= STARTF_USESTDHANDLES;
// at this moment, ProcessExplorer said 10 handles used
//
for(i=0; i<12; i++)
{
pInfo = (S_PI *) malloc(sizeof(S_PI));
if (CreateProcess(NULL, "c:/child.exe", NULL, NULL, FALSE, 0, NULL,
NULL, &si, &pi)==0)
{
puts("error");
exit(1);
}
pInfo->ProcessId = pi.dwProcessId;
pInfo->hProcess = pi.hProcess;
pInfo->hThread = pi.hThread;
b = _RegisterWaitForSingleObject(&pInfo->hNew, pi.hProcess, fct,
(PVOID) pInfo, INFINITE, 0x00000008);
}
while(1);
scanf(">>%d", &i);
FreeLibrary(h);
}
/**
* when the 12 children are stopped, I "stay" between 15/22 handles used
and
* never down to 10 handles !!
*
*/
void NTAPI fct(void *ptr, BOOLEAN TimerOrWaitFired)
{
S_PI *p = (S_PI *) ptr;
printf("child %d stops\n", p->ProcessId);
CloseHandle(p->hProcess);
CloseHandle(p->hThread);
_UnregisterWait(p->hNew);
free(ptr);
CloseHandle(p->hNew);
}
- Posted by Kellie Fitton on October 19th, 2005
Hi,
Where are using the API CloseHandle() for hProcess and hThread ??
Kellie.
- Posted by stef on October 19th, 2005
Hello,
I don't understand, CloseHandle() are used int fct() callback to free
hProcess() and hThread ??
Not the good place ??
- Posted by Lucian Wischik on October 20th, 2005
"stef" <stef.pellegrino@gmail.com> wrote:
Can you explain precisely what the handle loss is? -- I'd expect the
thread pool implementation to create more waitable threads internally,
and to keep handles for them internally, and not to release them until
your program terminates. So I think there's nothing to be worried
about.
Those last two lines are wrong in multiple ways. (1) you're not
allowed to access the memory after it's been freed. (2) you're not
allowed to CloseHandle() on a wait handle, like p->hNew. All you can
do is UnregisterWait it.
--
Lucian
- Posted by Lucian Wischik on October 20th, 2005
Lucian Wischik <lu.nn@wischik.com> wrote:
PS. I think your program would be simpler (easier to
understand/debug/maintain) if you didn't use the thread pool and
instead used WaitForMultipleObjects. But this is just opinion, and
others might disagree.
--
Lucian
- Posted by Kellie Fitton on October 21st, 2005
Hi,
I think you already freed the ptr, which is the "p" (free(ptr)),
and therefore the p->hNew canNot be referenced any more by the
API CloseHandle(p->hNew), so since the hNew is not available to
close, that will leak this handle for the calling thread.
Kellie.
- Posted by Lucian Wischik on October 21st, 2005
"Kellie Fitton" <KELLIEFITTON@YAHOO.COM> wrote:
p->hNew is a wait handle. The way to close these wait handles is with
UnregisterWait, which he already calls, so there is no leak.
Moreover, you can't use CloseHandle to close it -- "Note that a wait
handle cannot be used in functions that require an object handle, such
as CloseHandle."
http://msdn.microsoft.com/library/de...ngleobject.asp
--
Lucian
- Posted by jussij@zeusedit.com on October 23rd, 2005
What was the return value from the calls to CloseHandle?
Jussi Jumppanen
Author: Zeus for Windows IDE
http://www.zeusedit.com