Fix memory leak by properly closing the h5 file