]> git.sesse.net Git - stockfish/blobdiff - src/book.cpp
Be sure book file is closed before we leave
[stockfish] / src / book.cpp
index 8e4778eed711813b5ad6c3a46ff77b783aa859c9..c69eab739b7dfcae8284db5c417225ad466a831a 100644 (file)
@@ -344,10 +344,21 @@ namespace {
 ////
 
 
 ////
 
 
+/// Destructor. Be sure file is closed before we leave.
+
+Book::~Book() {
+
+  close();
+}
+
+
 /// Book::open() opens a book file with a given file name
 
 void Book::open(const string& fName) {
 
 /// Book::open() opens a book file with a given file name
 
 void Book::open(const string& fName) {
 
+  // Close old file before opening the new
+  close();
+
   fileName = fName;
   ifstream::open(fileName.c_str(), ifstream::in | ifstream::binary);
   if (!is_open())
   fileName = fName;
   ifstream::open(fileName.c_str(), ifstream::in | ifstream::binary);
   if (!is_open())
@@ -361,7 +372,6 @@ void Book::open(const string& fName) {
   if (!good())
   {
       cerr << "Failed to open book file " << fileName << endl;
   if (!good())
   {
       cerr << "Failed to open book file " << fileName << endl;
-      close();
       exit(EXIT_FAILURE);
   }
 }
       exit(EXIT_FAILURE);
   }
 }
@@ -479,7 +489,6 @@ void Book::read_entry(BookEntry& entry, int idx) {
   if (!good())
   {
       cerr << "Failed to read book entry at index " << idx << endl;
   if (!good())
   {
       cerr << "Failed to read book entry at index " << idx << endl;
-      close();
       exit(EXIT_FAILURE);
   }
 }
       exit(EXIT_FAILURE);
   }
 }