Shutting down cleanly.

What are some good reasons for shutting down cleanly?

By a clean exit I mean you've freed everything you've allocated, closed everything you've opened, shut down all your threads, etc.

I've been doing some work on some code trying to get it to shut down in a graceful manner and, as usual, it's not easy.. usually during development the focus is on 'getting it to work', and making sure everything is freed on the way out isn't that important anyway because the OS cleans up after you.  If you were listening on a socket, and you call ExitProcess(), the socket gets closed..

Here are some reasons I've come up with.

  • Leak detection is much easier if you can shut down cleanly, since anything you don't free is obvious.  Otherwise you have to do things like execute a code path 200 times and then look for those 200 allocations to stand out.

  • Do you have a resource that the OS isn't going to free automatically?  For example if you have a systray icon, and you exit without explicity removing it, it stays there until the user moves his mouse over it and then it disappears.

  • It's possible that someday you'll need to shut down and restart without actually exiting the process (ie, your code will become code that's called by someone else).  Then it will be important that you not leak.

Basically it just strikes me as good implementation to be able to shutdown cleanly.  Any other reasons?

The main reason for not bothering with a clean shutdown is that the OS does such a good job of cleaning up after you.