TerminateThread内存释放问题

donjin9 2014-06-19 04:57:47

#include <windows.h>
#include <process.h>

void Thread(void* p) {
//模拟阻塞函数,无法自行判断退出。
while (true) {
Sleep(1);
}
}

void main() {
while (true) {
HANDLE handle = (HANDLE)_beginthread(Thread, 0, NULL);
Sleep(100);
TerminateThread(handle, 0);
WaitForSingleObject(handle, INFINITE);
CloseHandle(handle);
}
}

xp系统,由于线程中调用了阻塞函数,因此先在主线程中判断,适当时强杀,但结果内存却狂飙。
帮忙看看上面的线程代码要怎样释放?
...全文
440 7 打赏 收藏 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-06-19
  • 打赏
  • 举报
回复
引用 6 楼 lianshaohua 的回复: 任何时候不要强杀,告诉它:你去死吧。然后它接到指令,喊完“XXXXX万岁!”后,自杀。
赵4老师 2014-06-19
  • 打赏
  • 举报
回复
理解和讨论之前请先学会如何观察!
donjin9 2014-06-19
  • 打赏
  • 举报
回复
我就是想问问有没方法手动去释放它的堆栈啥。
赵4老师 2014-06-19
  • 打赏
  • 举报
回复
参考 MSDN98\SAMPLES\VC98\SDK\WINBASE\IO\ASYNCIO\*.* http://bbs.csdn.net/topics/390374955
赵4老师 2014-06-19
  • 打赏
  • 举报
回复
TerminateThread The TerminateThread function terminates a thread. BOOL TerminateThread( HANDLE hThread, // handle to the thread DWORD dwExitCode // exit code for the thread ); Parameters hThread Handle to the thread to terminate. Windows NT: The handle must have THREAD_TERMINATE access. dwExitCode Specifies the exit code for the thread. Use the GetExitCodeThread function to retrieve a thread's exit value. Return Values If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError. Remarks TerminateThread is used to cause a thread to exit. When this occurs, the target thread has no chance to execute any user-mode code and its initial stack is not deallocated. DLLs attached to the thread are not notified that the thread is terminating. TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination. For example, TerminateThread can result in the following problems: If the target thread owns a critical section, the critical section will not be released. If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the thread's process could be inconsistent. If the target thread is manipulating the global state of a shared DLL, the state of the DLL could be destroyed, affecting other users of the DLL. A thread cannot protect itself against TerminateThread, other than by controlling access to its handles. The thread handle returned by the CreateThread and CreateProcess functions has THREAD_TERMINATE access, so any caller holding one of these handles can terminate your thread. If the target thread is the last thread of a process when this function is called, the thread's process is also terminated. The state of the thread object becomes signaled, releasing any other threads that had been waiting for the thread to terminate. The thread's termination status changes from STILL_ACTIVE to the value of the dwExitCode parameter. Terminating a thread does not necessarily remove the thread object from the system. A thread object is deleted when the last thread handle is closed. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Unsupported. Header: Declared in winbase.h. Import Library: Use kernel32.lib. See Also Processes and Threads Overview, Process and Thread Functions, CreateProcess, CreateThread, ExitThread, GetExitCodeThread
donjin9 2014-06-19
  • 打赏
  • 举报
回复
使用CloseHandle(handle);是因为如果不用,句柄数也在狂飙。 还有CreateThread也试过了,内存也在泄露。 有没有解决的办法呢?
赵4老师 2014-06-19
  • 打赏
  • 举报
回复
_beginthread 和 CreateThread 不是一回事!

65,213

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧