Use SCOPE_EXIT in write_gcore_file