1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-05-01 09:13:08 +00: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:
Marco Costalba 2009-05-29 17:23:21 +02:00
parent bafb9f1a25
commit 5b1316f7bb
3 changed files with 13 additions and 11 deletions

View file

@ -318,6 +318,7 @@ void Position::print(Move m) const {
void Position::copy(const Position& pos) { void Position::copy(const Position& pos) {
memcpy(this, &pos, sizeof(Position)); 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 /// inside startState and makes st point to it. This is needed
/// when the st pointee could become stale, as example because /// when the st pointee could become stale, as example because
/// the caller is about to going out of scope. /// 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 = &startState;
st->previous = NULL; // as a safe guard
} }

View file

@ -250,7 +250,7 @@ public:
bool square_is_weak(Square s, Color c) const; bool square_is_weak(Square s, Color c) const;
// Doing and undoing moves // Doing and undoing moves
void setStartState(const StateInfo& st); void saveState();
void do_move(Move m, StateInfo& st); void do_move(Move m, StateInfo& st);
void do_move(Move m, StateInfo& st, Bitboard dcCandidates); void do_move(Move m, StateInfo& st, Bitboard dcCandidates);
void undo_move(Move m); void undo_move(Move m);

View file

@ -210,9 +210,9 @@ namespace {
if (RootPosition.rule_50_counter() == 0) if (RootPosition.rule_50_counter() == 0)
RootPosition.reset_game_ply(); RootPosition.reset_game_ply();
} }
// Our StateInfo st is about going out of scope, // Our StateInfo st is about going out of scope so copy
// so save its content before they disappear. // its content inside RootPosition before they disappear.
RootPosition.setStartState(st); RootPosition.saveState();
} }
} }
} }