From fb0e19dc8bcd770db4f627b90697f29965151429 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sun, 25 Apr 2010 10:31:48 +0100 Subject: [PATCH] Do not call exit_threads() in Application d'tor Because exit_threads() references the global object TM, we need to call the function when still inside main(), otherwise, due to undefined global object initialization and destruction we could end up with referencing an already destroyed object. Actually this should not happen because Application singleton is initialized _only_ after all the other globals due to how Application::initialize() is defined, but this is very tricky C++ and not easy to follow, even for me ;-) Also rearranged a bit main() code flow. No functional change. Signed-off-by: Marco Costalba --- src/application.cpp | 16 +++++++++------- src/application.h | 2 +- src/evaluate.cpp | 2 +- src/main.cpp | 32 +++++++++++++++++--------------- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/application.cpp b/src/application.cpp index 31e12a9b..2d7a10bb 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -57,12 +57,6 @@ Application::Application() { genrand_int32(); } -Application::~Application() { - - exit_threads(); - quit_eval(); -} - void Application::initialize() { // A static Application object is allocated @@ -70,7 +64,15 @@ void Application::initialize() { static Application singleton; } +void Application::free_resources() { + + // Warning, following functions reference global objects that + // must be still alive when free_resources() is called. + exit_threads(); + quit_eval(); +} + void Application::exit_with_failure() { - exit(EXIT_FAILURE); // d'tor will be called automatically + exit(EXIT_FAILURE); } diff --git a/src/application.h b/src/application.h index 0c139a64..931e2e2b 100644 --- a/src/application.h +++ b/src/application.h @@ -29,10 +29,10 @@ class Application { Application(); Application(const Application&); - ~Application(); public: static void initialize(); + static void free_resources(); static void exit_with_failure(); }; diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 36f88f44..2b3335ca 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -382,7 +382,7 @@ Value do_evaluate(const Position& pos, EvalInfo& ei, int threadID) { // Middle-game specific evaluation terms if (phase > PHASE_ENDGAME) { - // Pawn storms in positions with opposite castling. + // Pawn storms in positions with opposite castling if ( square_file(pos.king_square(WHITE)) >= FILE_E && square_file(pos.king_square(BLACK)) <= FILE_D) diff --git a/src/main.cpp b/src/main.cpp index b4870606..e623297c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -57,8 +57,19 @@ int main(int argc, char *argv[]) { CALLGRIND_START_INSTRUMENTATION; #endif - // Process command line arguments if any - if (argc > 1) + if (argc <= 1) + { + // Print copyright notice + cout << engine_name() + << " by Tord Romstad, Marco Costalba, Joona Kiiski" << endl; + + if (CpuHasPOPCNT) + cout << "Good! CPU has hardware POPCNT." << endl; + + // Enter UCI mode + uci_main_loop(); + } + else // Process command line arguments { if (string(argv[1]) != "bench" || argc < 4 || argc > 8) cout << "Usage: stockfish bench " @@ -68,22 +79,13 @@ int main(int argc, char *argv[]) { else { string time = argc > 4 ? argv[4] : "60"; - string fen = argc > 5 ? argv[5] : "default"; - string lim = argc > 6 ? argv[6] : "time"; - string tim = argc > 7 ? argv[7] : ""; + string fen = argc > 5 ? argv[5] : "default"; + string lim = argc > 6 ? argv[6] : "time"; + string tim = argc > 7 ? argv[7] : ""; benchmark(string(argv[2]) + " " + string(argv[3]) + " " + time + " " + fen + " " + lim + " " + tim); } - return 0; } - // Print copyright notice - cout << engine_name() - << ". By Tord Romstad, Marco Costalba, Joona Kiiski." << endl; - - if (CpuHasPOPCNT) - cout << "Good! CPU has hardware POPCNT. We will use it." << endl; - - // Enter UCI mode - uci_main_loop(); + Application::free_resources(); return 0; } -- 2.39.2