1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

Use make_unique for TurnInfo in CGameHandler.

* Fixes use after free @CGameHandler.cpp:2093
This commit is contained in:
AlexVinS 2016-08-17 08:53:36 +03:00
parent b0045fa357
commit e7f2858d33

View File

@ -1539,10 +1539,9 @@ void CGameHandler::newTurn()
NewTurn::Hero hth;
hth.id = h->id;
auto ti = new TurnInfo(h, 1);
auto ti = make_unique<TurnInfo>(h, 1);
// TODO: this code executed when bonuses of previous day not yet updated (this happen in NewTurn::applyGs). See issue 2356
hth.move = h->maxMovePoints(gs->map->getTile(h->getPosition(false)).terType != ETerrainType::WATER, ti);
delete ti;
hth.move = h->maxMovePoints(gs->map->getTile(h->getPosition(false)).terType != ETerrainType::WATER, ti.get());
hth.mana = h->getManaNewTurn();
n.heroes.insert(hth);
@ -1994,11 +1993,10 @@ bool CGameHandler::moveHero( ObjectInstanceID hid, int3 dst, ui8 teleporting, bo
tmh.movePoints = h->movement;
//check if destination tile is available
auto ti = new TurnInfo(h);
auto ti = make_unique<TurnInfo>(h);
const bool canFly = ti->hasBonusOfType(Bonus::FLYING_MOVEMENT);
const bool canWalkOnSea = ti->hasBonusOfType(Bonus::WATER_WALKING);
const int cost = CPathfinderHelper::getMovementCost(h, h->getPosition(), hmpos, nullptr, nullptr, h->movement, ti);
delete ti;
const int cost = CPathfinderHelper::getMovementCost(h, h->getPosition(), hmpos, nullptr, nullptr, h->movement, ti.get());
//it's a rock or blocked and not visitable tile
//OR hero is on land and dest is water and (there is not present only one object - boat)
@ -2090,14 +2088,14 @@ bool CGameHandler::moveHero( ObjectInstanceID hid, int3 dst, ui8 teleporting, bo
if(!transit && embarking)
{
tmh.movePoints = h->movementPointsAfterEmbark(h->movement, cost, false, ti);
tmh.movePoints = h->movementPointsAfterEmbark(h->movement, cost, false, ti.get());
return doMove(TryMoveHero::EMBARK, IGNORE_GUARDS, DONT_VISIT_DEST, LEAVING_TILE);
// In H3 embark ignore guards
}
if(disembarking)
{
tmh.movePoints = h->movementPointsAfterEmbark(h->movement, cost, true, ti);
tmh.movePoints = h->movementPointsAfterEmbark(h->movement, cost, true, ti.get());
return doMove(TryMoveHero::DISEMBARK, CHECK_FOR_GUARDS, VISIT_DEST, LEAVING_TILE);
}