]> git.sesse.net Git - stockfish/commitdiff
Fix a Windows-only crash on exit without 'quit'
authorSami Kiminki <skiminki@users.noreply.github.com>
Thu, 14 May 2020 09:00:35 +0000 (12:00 +0300)
committerJoost VandeVondele <Joost.VandeVondele@gmail.com>
Thu, 14 May 2020 18:35:40 +0000 (20:35 +0200)
There was a bug in commit d4763424d2728fe2dfd0a6fe747666feb6a2fdbb
(Add support for Windows large pages) that could result in trying to
free memory allocated with VirtualAlloc incorrectly with free().

Fix this by reverting the TT.resize(0) logic in the previous commit,
and instead, just call aligned_ttmem_free() in
TranspositionTable::~TranspositionTable().

fixes https://github.com/official-stockfish/Stockfish/issues/2677

closes https://github.com/official-stockfish/Stockfish/pull/2679

No functional change

src/main.cpp
src/misc.cpp
src/misc.h
src/tt.cpp
src/tt.h

index c7cf2c6f28fddb05581667ffc16a5dca5d35657e..6eeda66dff805e7672bc83eef5c342799398ed72 100644 (file)
@@ -49,7 +49,6 @@ int main(int argc, char* argv[]) {
 
   UCI::loop(argc, argv);
 
 
   UCI::loop(argc, argv);
 
-  TT.resize(0);
   Threads.set(0);
   return 0;
 }
   Threads.set(0);
   return 0;
 }
index b1c0feeb9e139b6b91476663d5a5987872746c0e..e0cc6ed5977d9d39b838d5cf99e6ea1d92777f37 100644 (file)
@@ -390,7 +390,7 @@ void* aligned_ttmem_alloc(size_t allocSize, void*& mem) {
 
 void aligned_ttmem_free(void* mem) {
 
 
 void aligned_ttmem_free(void* mem) {
 
-  if (!VirtualFree(mem, 0, MEM_RELEASE))
+  if (mem && !VirtualFree(mem, 0, MEM_RELEASE))
   {
       DWORD err = GetLastError();
       std::cerr << "Failed to free transposition table. Error code: 0x" <<
   {
       DWORD err = GetLastError();
       std::cerr << "Failed to free transposition table. Error code: 0x" <<
index 9d53c2dab129220f37bf01f273db05048f0fb97b..05bfc7de4d56906bb436d3bfe634c4e8dce4284b 100644 (file)
@@ -34,7 +34,7 @@ const std::string compiler_info();
 void prefetch(void* addr);
 void start_logger(const std::string& fname);
 void* aligned_ttmem_alloc(size_t size, void*& mem);
 void prefetch(void* addr);
 void start_logger(const std::string& fname);
 void* aligned_ttmem_alloc(size_t size, void*& mem);
-void aligned_ttmem_free(void* mem);
+void aligned_ttmem_free(void* mem); // nop if mem == nullptr
 
 void dbg_hit_on(bool b);
 void dbg_hit_on(bool c, bool b);
 
 void dbg_hit_on(bool b);
 void dbg_hit_on(bool c, bool b);
index 6ee63138d15497851843ce114108cddba262151e..4e06bed93451d867fdc49e4d6c11fc26b0ad27ef 100644 (file)
@@ -63,14 +63,7 @@ void TranspositionTable::resize(size_t mbSize) {
 
   Threads.main()->wait_for_search_finished();
 
 
   Threads.main()->wait_for_search_finished();
 
-  if (mem)
-      aligned_ttmem_free(mem);
-
-  if (!mbSize)
-  {
-      mem = nullptr;
-      return;
-  }
+  aligned_ttmem_free(mem);
 
   clusterCount = mbSize * 1024 * 1024 / sizeof(Cluster);
   table = static_cast<Cluster*>(aligned_ttmem_alloc(clusterCount * sizeof(Cluster), mem));
 
   clusterCount = mbSize * 1024 * 1024 / sizeof(Cluster);
   table = static_cast<Cluster*>(aligned_ttmem_alloc(clusterCount * sizeof(Cluster), mem));
index 8b70f79787619ca4c1b6326f55cfa456a2dcf04f..bd723a867902ab4011e12fe2dee00e9e59e8242c 100644 (file)
--- a/src/tt.h
+++ b/src/tt.h
@@ -75,7 +75,7 @@ class TranspositionTable {
   static_assert(sizeof(Cluster) == 32, "Unexpected Cluster size");
 
 public:
   static_assert(sizeof(Cluster) == 32, "Unexpected Cluster size");
 
 public:
- ~TranspositionTable() { free(mem); }
+ ~TranspositionTable() { aligned_ttmem_free(mem); }
   void new_search() { generation8 += 8; } // Lower 3 bits are used by PV flag and Bound
   TTEntry* probe(const Key key, bool& found) const;
   int hashfull() const;
   void new_search() { generation8 += 8; } // Lower 3 bits are used by PV flag and Bound
   TTEntry* probe(const Key key, bool& found) const;
   int hashfull() const;