1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

Use bonus only as shared_ptr to avoid memory corruption

This commit is contained in:
Ivan Savenko
2025-06-16 16:22:21 +03:00
parent 4044e03c0a
commit 463c404a83
19 changed files with 62 additions and 67 deletions

View File

@@ -152,24 +152,24 @@ void Rewardable::Interface::grantRewardAfterLevelup(IGameEventCallback & gameEve
gameEvents.setMovePoints(&smp);
}
for(const Bonus & bonus : info.reward.heroBonuses)
for(const auto & bonus : info.reward.heroBonuses)
{
GiveBonus gb(GiveBonus::ETarget::OBJECT, hero->id, bonus);
GiveBonus gb(GiveBonus::ETarget::OBJECT, hero->id, *bonus);
gameEvents.giveHeroBonus(&gb);
}
if (hero->getCommander())
{
for(const Bonus & bonus : info.reward.commanderBonuses)
for(const auto & bonus : info.reward.commanderBonuses)
{
GiveBonus gb(GiveBonus::ETarget::HERO_COMMANDER, hero->id, bonus);
GiveBonus gb(GiveBonus::ETarget::HERO_COMMANDER, hero->id, *bonus);
gameEvents.giveHeroBonus(&gb);
}
}
for(const Bonus & bonus : info.reward.playerBonuses)
for(const auto & bonus : info.reward.playerBonuses)
{
GiveBonus gb(GiveBonus::ETarget::PLAYER, hero->getOwner(), bonus);
GiveBonus gb(GiveBonus::ETarget::PLAYER, hero->getOwner(), *bonus);
gameEvents.giveHeroBonus(&gb);
}

View File

@@ -81,10 +81,10 @@ void Rewardable::Reward::loadComponents(std::vector<Component> & comps, const CG
for (auto & bonus : heroBonuses)
{
if (bonus.type == BonusType::MORALE)
comps.emplace_back(ComponentType::MORALE, bonus.val);
if (bonus.type == BonusType::LUCK)
comps.emplace_back(ComponentType::LUCK, bonus.val);
if (bonus->type == BonusType::MORALE)
comps.emplace_back(ComponentType::MORALE, bonus->val);
if (bonus->type == BonusType::LUCK)
comps.emplace_back(ComponentType::LUCK, bonus->val);
}
if (heroExperience)

View File

@@ -86,9 +86,9 @@ struct DLL_LINKAGE Reward final
std::vector<CStackBasicDescriptor> guards;
/// list of bonuses, e.g. morale/luck
std::vector<Bonus> heroBonuses;
std::vector<Bonus> commanderBonuses;
std::vector<Bonus> playerBonuses;
std::vector<std::shared_ptr<Bonus>> heroBonuses;
std::vector<std::shared_ptr<Bonus>> commanderBonuses;
std::vector<std::shared_ptr<Bonus>> playerBonuses;
/// skills that hero may receive or lose
std::vector<si32> primary;