Close resource leak in temporary file manager.

rebase of #730 on `dev_4_4`