
A trick I use to deal with this problem is to generate the files with a .embryo extension and once I know the file has been successfully written out and closed I perform a rename (which is atomic) to get rid of the .embryo. The next time the application is run, it can cleanup any .embryo files on startup (or just ignore and overwrite them). It's also possible to add uncaught exception handlers and shutdown hooks to try to clean up the incomplete files, but this can be tricky to get right, and it's often it's better to leave them there for diagnostic purposes anyway.
1. Could mkgmap clean up broken 812KB mdr_img files if creating crashes due to insufficient memory? This is a potential threat as the _mdr.img is broken but hard to notice if you don't look at error notices).