mirror of
https://github.com/HChaZZY/Stockfish.git
synced 2025-12-17 07:36:23 +08:00
Detach the state when copying a position
In Position we store a pointer to a StateInfo record kept outside of the Position object. When copying a position we copy also that pointer so after the copy we have two Position objects pointing to the same StateInfo record. This can be dangerous so fix by copying also the StateInfo record inside the new Position object and let the new st pointer point to it. This completely detach the copied Position from the original one. Also rename setStartState() as saveState() and clean up the API to state more clearly what the function does. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
@@ -315,9 +315,10 @@ void Position::print(Move m) const {
|
||||
|
||||
/// Position::copy() creates a copy of the input position.
|
||||
|
||||
void Position::copy(const Position &pos) {
|
||||
void Position::copy(const Position& pos) {
|
||||
|
||||
memcpy(this, &pos, sizeof(Position));
|
||||
saveState(); // detach and copy state info
|
||||
}
|
||||
|
||||
|
||||
@@ -1607,15 +1608,16 @@ int Position::see(Square from, Square to) const {
|
||||
}
|
||||
|
||||
|
||||
/// Position::setStartState() copies the content of the argument
|
||||
/// Position::saveState() copies the content of the current state
|
||||
/// inside startState and makes st point to it. This is needed
|
||||
/// when the st pointee could become stale, as example because
|
||||
/// the caller is about to going out of scope.
|
||||
|
||||
void Position::setStartState(const StateInfo& s) {
|
||||
void Position::saveState() {
|
||||
|
||||
startState = s;
|
||||
startState = *st;
|
||||
st = &startState;
|
||||
st->previous = NULL; // as a safe guard
|
||||
}
|
||||
|
||||
|
||||
@@ -1966,7 +1968,7 @@ void Position::init_piece_square_tables() {
|
||||
/// the white and black sides reversed. This is only useful for debugging,
|
||||
/// especially for finding evaluation symmetry bugs.
|
||||
|
||||
void Position::flipped_copy(const Position &pos) {
|
||||
void Position::flipped_copy(const Position& pos) {
|
||||
|
||||
assert(pos.is_ok());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user