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

@@ -142,15 +142,10 @@ std::shared_ptr<CArtifact> CArtHandler::loadFromJson(const std::string & scope,
if(!node["growing"].isNull())
{
for(auto bonus : node["growing"]["bonusesPerLevel"].Vector())
{
art->bonusesPerLevel.emplace_back(static_cast<ui16>(bonus["level"].Float()), Bonus());
JsonUtils::parseBonus(bonus["bonus"], &art->bonusesPerLevel.back().second);
}
art->bonusesPerLevel.emplace_back(static_cast<ui16>(bonus["level"].Float()), JsonUtils::parseBonus(bonus["bonus"]));
for(auto bonus : node["growing"]["thresholdBonuses"].Vector())
{
art->thresholdBonuses.emplace_back(static_cast<ui16>(bonus["level"].Float()), Bonus());
JsonUtils::parseBonus(bonus["bonus"], &art->thresholdBonuses.back().second);
}
art->thresholdBonuses.emplace_back(static_cast<ui16>(bonus["level"].Float()), JsonUtils::parseBonus(bonus["bonus"]));
}
art->id = ArtifactID(index);

View File

@@ -59,22 +59,22 @@ bool CGrowingArtifact::isGrowing() const
return !bonusesPerLevel.empty() || !thresholdBonuses.empty();
}
std::vector <std::pair<ui16, Bonus>> & CGrowingArtifact::getBonusesPerLevel()
std::vector <std::pair<ui16, std::shared_ptr<Bonus>>> & CGrowingArtifact::getBonusesPerLevel()
{
return bonusesPerLevel;
}
const std::vector <std::pair<ui16, Bonus>> & CGrowingArtifact::getBonusesPerLevel() const
const std::vector <std::pair<ui16, std::shared_ptr<Bonus>>> & CGrowingArtifact::getBonusesPerLevel() const
{
return bonusesPerLevel;
}
std::vector <std::pair<ui16, Bonus>> & CGrowingArtifact::getThresholdBonuses()
std::vector <std::pair<ui16, std::shared_ptr<Bonus>>> & CGrowingArtifact::getThresholdBonuses()
{
return thresholdBonuses;
}
const std::vector <std::pair<ui16, Bonus>> & CGrowingArtifact::getThresholdBonuses() const
const std::vector <std::pair<ui16, std::shared_ptr<Bonus>>> & CGrowingArtifact::getThresholdBonuses() const
{
return thresholdBonuses;
}

View File

@@ -54,15 +54,15 @@ class DLL_LINKAGE CGrowingArtifact
protected:
CGrowingArtifact() = default;
std::vector<std::pair<ui16, Bonus>> bonusesPerLevel; // Bonus given each n levels
std::vector<std::pair<ui16, Bonus>> thresholdBonuses; // After certain level they will be added once
std::vector<std::pair<ui16, std::shared_ptr<Bonus>>> bonusesPerLevel; // Bonus given each n levels
std::vector<std::pair<ui16, std::shared_ptr<Bonus>>> thresholdBonuses; // After certain level they will be added once
public:
bool isGrowing() const;
std::vector<std::pair<ui16, Bonus>> & getBonusesPerLevel();
const std::vector<std::pair<ui16, Bonus>> & getBonusesPerLevel() const;
std::vector<std::pair<ui16, Bonus>> & getThresholdBonuses();
const std::vector<std::pair<ui16, Bonus>> & getThresholdBonuses() const;
std::vector<std::pair<ui16, std::shared_ptr<Bonus>>> & getBonusesPerLevel();
const std::vector<std::pair<ui16, std::shared_ptr<Bonus>>> & getBonusesPerLevel() const;
std::vector<std::pair<ui16, std::shared_ptr<Bonus>>> & getThresholdBonuses();
const std::vector<std::pair<ui16, std::shared_ptr<Bonus>>> & getThresholdBonuses() const;
};
class DLL_LINKAGE CChargedArtifact

View File

@@ -109,7 +109,7 @@ void CGrowingArtifactInstance::growingUp()
// Every n levels
if(artInst->valOfBonuses(BonusType::ARTIFACT_GROWING) % bonus.first == 0)
{
artInst->accumulateBonus(std::make_shared<Bonus>(bonus.second));
artInst->accumulateBonus(std::make_shared<Bonus>(*bonus.second));
}
}
for(const auto & bonus : artType->getThresholdBonuses())
@@ -117,7 +117,7 @@ void CGrowingArtifactInstance::growingUp()
// At n level
if(artInst->valOfBonuses(BonusType::ARTIFACT_GROWING) == bonus.first)
{
artInst->addNewBonus(std::make_shared<Bonus>(bonus.second));
artInst->addNewBonus(std::make_shared<Bonus>(*bonus.second));
}
}