Fix core dump when closing same file twice