From e953b4c9ca8bd875a2a675e7794fe0e7cf29526c Mon Sep 17 00:00:00 2001 From: Yann Dirson Date: Sun, 20 Apr 2008 21:52:42 +0200 Subject: [PATCH] Switch to manual memory management in legal(). The auto_ptr-based version was not working as expected, and manual management is quite easy to do in this case. --- src/variants/chess/validator.cpp | 17 ++++++++++++----- src/variants/shogi/validator.cpp | 17 ++++++++++++----- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/variants/chess/validator.cpp b/src/variants/chess/validator.cpp index f652226..f76950d 100644 --- a/src/variants/chess/validator.cpp +++ b/src/variants/chess/validator.cpp @@ -67,17 +67,24 @@ bool Validator::legal(const IState* state, Move& move) const { const IColor* turn = m_delegator->mover(state, move); - std::auto_ptr tmp(state->clone()); + IState* tmp = state->clone(); + Q_ASSERT(tmp); tmp->move(move); Point kingPos = tmp->board()->find(Piece(turn, King::self())); - if (kingPos == Point::invalid()) + if (kingPos == Point::invalid()) { + delete tmp; return false; - - if (m_delegator->attacks(tmp.get(), behaviour->opponent(turn), kingPos)) + } + + if (m_delegator->attacks(tmp, behaviour->opponent(turn), kingPos)) { + delete tmp; return false; - + } + + delete tmp; + // set move type as normal, if not already set if (move.type().isEmpty()) move.setType("normal"); return true; diff --git a/src/variants/shogi/validator.cpp b/src/variants/shogi/validator.cpp index ba6267a..b3c2ddc 100644 --- a/src/variants/shogi/validator.cpp +++ b/src/variants/shogi/validator.cpp @@ -90,17 +90,24 @@ bool Validator::legal(const IState* state, Move& move) const { const IColor* turn = m_delegator->mover(state, move); - std::auto_ptr tmp(state->clone()); + IState* tmp = state->clone(); + Q_ASSERT(tmp); tmp->move(move); Point kingPos = tmp->board()->find(Piece(turn, King::self())); - if (kingPos == Point::invalid()) + if (kingPos == Point::invalid()) { + delete tmp; return false; - - if (m_delegator->attacks(tmp.get(), behaviour->opponent(turn), kingPos)) + } + + if (m_delegator->attacks(tmp, behaviour->opponent(turn), kingPos)) { + delete tmp; return false; - + } + + delete tmp; + // set move type as normal, if not already set if (move.type().isEmpty()) move.setType("normal"); return true; -- 2.11.4.GIT