Fixed double free by avoiding calling fclose twice.