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

Fix potential BattleInfo destruction before usage

This commit is contained in:
nordsoft 2022-11-06 21:46:56 +04:00
parent dcda359567
commit 4ccad9178e
4 changed files with 15 additions and 15 deletions

View File

@ -1465,8 +1465,6 @@ struct BattleResult : public Query
exp[1] = 0;
};
void applyFirstCl(CClient *cl);
void applyGs(CGameState *gs);
EResult result;
ui8 winner; //0 - attacker, 1 - defender, [2 - draw (should be possible?)]

View File

@ -1385,17 +1385,6 @@ DLL_LINKAGE void BattleUpdateGateState::applyGs(CGameState *gs)
gs->curB->si.gateState = state;
}
void BattleResult::applyGs(CGameState *gs)
{
for (auto & elem : gs->curB->stacks)
delete elem;
for(int i = 0; i < 2; i++)
gs->curB->battleGetArmyObject(i)->battle = nullptr;
gs->curB.dellNull();
}
void BattleResultAccepted::applyGs(CGameState * gs)
{
for(auto * h : {hero1, hero2})
@ -1426,6 +1415,8 @@ void BattleResultAccepted::applyGs(CGameState * gs)
CBonusSystemNode::treeHasChanged();
}
gs->curB.dellNull();
}
DLL_LINKAGE void BattleLogMessage::applyGs(CGameState *gs)

View File

@ -539,7 +539,15 @@ BattleInfo::BattleInfo():
setNodeType(BATTLE);
}
BattleInfo::~BattleInfo() = default;
BattleInfo::~BattleInfo()
{
for (auto & elem : stacks)
delete elem;
for(int i = 0; i < 2; i++)
if(auto * _armyObj = battleGetArmyObject(i))
_armyObj->battle = nullptr;
}
int32_t BattleInfo::getActiveStackID() const
{

View File

@ -2707,6 +2707,9 @@ void CGameHandler::startBattlePrimary(const CArmedInstance *army1, const CArmedI
const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool creatureBank,
const CGTownInstance *town) //use hero=nullptr for no hero
{
if(gs->curB)
gs->curB.dellNull();
engageIntoBattle(army1->tempOwner);
engageIntoBattle(army2->tempOwner);