Fix silly use-after-free in file write logic