Close thread handle by thread id

Close thread handle by thread id

Post by kevi » Fri, 07 Nov 2003 10:46:49

I want to close a thread handle, but I cannot get it from
createthread(). What I can get is threadid from threadproc
(). In fact, this thread is created in a library, but they
forget to close handle. I try to use openthread() or
duplicatehandle to get a new handle and close the new one,
but I found the new one is a cloned one, not the original
one. I still cannot close the original handle, how to do

Close thread handle by thread id

Post by Gary Chans » Fri, 07 Nov 2003 13:47:55

I think the only place you can get that exact handle is where that
thread is created. You may have to do something messy like patching the
calling code to attach your own hooks.


-GJC [MS Windows SDK MVP]
-Software Consultant (Embedded systems and Real Time Controls)

-Abolish public schools


Close thread handle by thread id

Post by Kirk Ferdm » Fri, 07 Nov 2003 14:02:14

Of course it's a new one. You need to close the original thread handle. The
one that was returned by CreateThread API. I understand that you use 3d
party library and it leaks. If you have source code for the library try to
find what they do with the original handle. The variable might be visiable
to the calling code and thus you can call CloseHandle on it. In any case
send a bug report to the author.


Close thread handle by thread id

Post by Farooque K » Fri, 07 Nov 2003 14:13:00

I think CreateThread() returns the thread handle.

OpenThread() or DuplicateThread() won't work in your case.


-Farooque Khan

Close thread handle by thread id

Post by Jochen Kal » Fri, 07 Nov 2003 15:17:25

What is "createthread" !?
If this function does not return the handle it MUST close it internally...

In Win API there is only one Function to create a thread: CreateThread

In Win CRT there are two: beginthread (which does not return the handle)
and beginthreadex (which returns the handle)


Do you need a memory-leak finder ?