Double fclose caused segfault