What happens to a process handle once the process was ended?

岁酱吖の 提交于 2019-12-06 11:28:11

The handle indirectly points to an kernel object. As long as there are open handles, the object will be kept alive.

Will the handle (or better the memory behind it) be re-used for another process?

The numeric value of the handle (or however it is implemented) might get reused, but that doesn't mean it'll always point to the same thing. Just like process IDs.

Or will GetExitCodeProcess() for example get the correct result forever from now on?

No. When all handles to the process are closed, the process object is freed (along with its exit code). Note that running process holds an implicit handle to itself. You can hold an open handle, though, as long as you need it.

If 2. is true: Wouldn't that mean that I can bring down the OS with starting/killing new processes, since I create more and more handles (and the OS reserves memory for them)?

There are many ways to starve the system. It will either start heavily swapping or just fail to spawn a new process at some point.

Short answer:

GetExitCodeProcess works until you call CloseHandle, after what the process object will be released and may be reused.

Long answer: See Cat Plus Plus's answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!