mirror of
https://github.com/HChaZZY/Stockfish.git
synced 2025-12-18 08:07:08 +08:00
Correctly handle handover of setup states
Before the search we setup the starting position doing all the moves (sent by GUI) from start position to the position just before to start searching. To do this we use a set of StateInfo records used by each do_move() call. These records shall be kept valid during all the search because repetition draw detection uses them to back track all the earlier positions keys. The problem is that, while searching, the GUI could send another 'position' command, this calls set_position() that clears the states! Of course a crash follows shortly. Before searching all the relevant parameters are copied in start_searching() just for this reason: to fully detach data accessed during the search from the UCI protocol handling. So the natural solution would be to copy also the setup states. Unfortunatly this approach does not work because StateInfo contains a pointer to the previous record, so naively copying and then freeing the original memory leads to a crash. That's why we use two std::auto_ptr (one belonging to UCI and another to Search) to safely transfer ownership of the StateInfo records to the search, after we have setup the root position. As a nice side-effect all the possible memory leaks are magically sorted out for us by std::auto_ptr semantic. No functional change. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
@@ -414,7 +414,7 @@ void ThreadPool::wait_for_search_finished() {
|
||||
// a new search, then returns immediately.
|
||||
|
||||
void ThreadPool::start_searching(const Position& pos, const LimitsType& limits,
|
||||
const std::vector<Move>& searchMoves) {
|
||||
const std::vector<Move>& searchMoves, StateStackPtr& states) {
|
||||
wait_for_search_finished();
|
||||
|
||||
SearchTime.restart(); // As early as possible
|
||||
@@ -424,6 +424,7 @@ void ThreadPool::start_searching(const Position& pos, const LimitsType& limits,
|
||||
|
||||
RootPosition = pos;
|
||||
Limits = limits;
|
||||
SetupStates = states; // Ownership transfer here
|
||||
RootMoves.clear();
|
||||
|
||||
for (MoveList<LEGAL> ml(pos); !ml.end(); ++ml)
|
||||
|
||||
Reference in New Issue
Block a user