问题
I have Delphi application on windows XP. This application works with 100 threads. At some cases i have to terminate thread with fully drops of thread's stack.
I looking for function from WinAPI like terminatethread but it not safelly and: Windows Server 2003 and Windows XP/2000: The target thread's initial stack is not freed, causing a resource leak.
回答1:
There's no such function. No such function could possibly exist. Windows cannot possibly know which resources are to be tidied up by that thread, and it couldn't know how to do so. That is the responsibilty of your app.
It sounds to me like your are going about this the wrong way. You ought to be terminating the thread in a co-operative fashion without using force. Anything else is bound to lead to leaks and most likely other more serious problems.
回答2:
If your application threads are all written in Delphi code, then consider using OmniThreadLibrary, and using its provided services, instead of raw Win32 APIs, or TThread's built in functionality, which is pretty decent in recent versions of Delphi.
However, for signalling threads and shutting them down, especially where not all the threads are created and managed by Delphi THread objects, a great deal of design will have to be done by you, and you will probably have to combine a few Win32 primitives together to build a decent solution. I would use a Win32 event and signal threads written in C/C++ using the Win32 event handle, for example.
For a pure 100% delphi codebase, I would simply use whatever is well tested, and use the TThread APIs or OmniThreadLibrary, rather than going to the raw Win32 API.
来源:https://stackoverflow.com/questions/6452486/windows-api-safe-termination-of-thread