Fixed cases where file would be opened, but not closed.

Also one memory issue.
These are very small issues, but I noticed them with cppcheck, in my own project.