Fixed memory leaks

Fixed memory leaks of the TFile and in the I/O code.