Saturday 29 August 2015

How to exit a process on Windows

Reading Nethack 4's blog on this subject I wanted to be a bad person and tell how you can end a process in Windows (using Visual C++ of course). You should start the project with F5 to run in debug mode.

In case you get into a infinite loop you can exit the process with shift+F5 (debug - stop debugging in VC).

From programming perspective loops are something to watch out. It can be a typical programmer's idea to assume that a loop always has an exit condition. Just as it's difficult to control the index value of an array it's also sometimes hard to keep track of the exit condition. I'm always programming a panic exit logic in loops. It may sound like overdoing it, but it's a really good idea. Panic exit logic is a counter with max value which runs along the loop and exits when the max value is reached. For debug purposes it's also good to inform when panic exit was the result of the loop to find out the reason for the infinite condition.

Sometimes resource leaks in a loop can make the OS very slow. It happens in Windows, too. Programmers should be extra careful when there are resource management in a loop which I think sounds a bit odd, but then again I have more experience from C++ resource management which mostly happens in constructors and destructors - not in loops. This I think is another good thing about C++, because it's less likely to get into strange race conditions if the class hierarchy is a tree-like system where everything is released in a strict order.

This is getting into another topic, but C++ is not protected from certain type of leaks. When you think of dangerous things in C++ (which in C are typical everyday solutions) there are three things to be extra careful: passing and returning a new object and creating a clone of an object. In C++ returning a new object from a function is not a common style (and should be avoided), but passing object to another container is, as is creating a clone. You need to be careful to handle the ownership change (as it is called in C++) properly to avoid losing the object (and thus failing to release it).

No comments: