To make sure that filehandles are closed properly I've lately written > with_file functions that take a filename and a function. > It then opens the file, invokes the function (in a try block), and closes > the file (even if the try block threw an exception). > That way no file handles leak, and the GC is not needed to clean up. > > This is in some ways similar to RAII in C++ where destructors > automatically clean up resources when you leave a scope. > Maybe there should be a small library that provides these convenience > functions (other resources can be handled similarly, > opendir/closedir comes to mind, but there are probably other cases where > an explicit close-like functionality is needed). > Those functions are defined in Batteries, see BatPervasives.with_dispose ( http://ocaml-batteries-team.github.com/batteries-included/hdoc/BatPervasives.html), BatFile.with_file_in and BatFile.with_file_out ( http://ocaml-batteries-team.github.com/batteries-included/hdoc/BatFile.html ). They are indeed very useful ! Cheers, ph.