process termination

Started by
3 comments, last by Anon Mike 18 years, 7 months ago
I've some coding skill regarding Linux handling over processes and thread. Now I need to work in a game in m/Windows (.+)/ plaform and I'm a little confused regarding process kill. I've already managed WM_CLOSE, WM_QUIT, WM_WHATEVER event together with the atexit function and eventually signal(SIG_TERM and other), ctrl+C etc. etc. and everything runs well with my program that release all resource in a good way at process exceptional exit. But now I need to manage the evil: ctrl+alt+canc -> task manager -> (terminate process| terminate process structure). There is a way to manage this event? When I kill a process in this way NO signal is raised, NO atexit function are called, NO structured exception is raised, NO exception, well NOTHING seems to happen in the killed process so I can't do any resouce collect and I have memory leak. Do Ms has implemented this danger option without any form of notification to the killed process? I can't believe this. Am I missing something?
[ILTUOMONDOFUTURO]
Advertisement
too difficult question? :)
any help will be greatly appreciated...
[ILTUOMONDOFUTURO]
Are you sure you have a memory leak? When a process is terminated from the process list, Windows deallocats the chunk(s) of memory requested by the process, and removes it from the process "queue" or tree, or whatever it uses, including all threads which are associated with it. If you're using malloc or new, then that memory is allocated on the heap, and the entire heap should be "removed" when Windows deallocates the memory. If you're allocating a new heap through Windows, it should still be removed from memory, though I'm not sure about it.

Nevertheless, I wouldn't be too worried about any leak resulting from this form of shutdown. This is an emergency shutdown, usually when your program locks up or when people get fed up of waiting, and the End Task button (which I think does send a WM_QUIT message) doesn't work, at least in a timely manner. As such, people who use this should be aware that it could adversely affect system performance. Most users don't even know that little tab exists, or how to use it anyway. ^_^

Theoretically, you should be able to detect it using Windows Hooks, but that would probably be more work than it's worth. Plus, I can't tell you how to do it either.
But what about function that are called at program exit? Something like flush the file or save setting? These will never be called.
[ILTUOMONDOFUTURO]
If somebody calls TerminateProcess on you then you're dead and you get no chance to handle things. The OS will clean up your memory, close your handles, etc, etc so you don't need to worry about memory leaks. TerminateProcess is basically the last ditch effort to get the process to go away after posting window messages and ctrl-c, etc have failed.

The only place you might run into trouble is if your program leaves turds around like flag files or temporary data in the registry or something. The answer to those is simple - don't do that. The other possible gotcha is if you're talking over the network via a UDP "connection" but in that case you should have some sort of timeout mechanism on the remote machine anyway.
-Mike

This topic is closed to new replies.

Advertisement