1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-26 03:52:01 +02:00

Try to simplify / cleanup code to localize crash

This commit is contained in:
Ivan Savenko 2024-01-20 18:40:03 +02:00
parent 912262e826
commit 8e8d42bfa2
4 changed files with 14 additions and 23 deletions

View File

@ -21,17 +21,7 @@
VCMI_LIB_NAMESPACE_BEGIN
AObjectTypeHandler::AObjectTypeHandler() = default;
AObjectTypeHandler::~AObjectTypeHandler()
{
// FIXME: currently on Android there is a weird crash in destructor of 'base' member
// this code attempts to localize and fix this crash
if (base)
{
base->clear();
base.reset();
}
}
AObjectTypeHandler::~AObjectTypeHandler() = default;
std::string AObjectTypeHandler::getJsonKey() const
{
@ -89,12 +79,12 @@ void AObjectTypeHandler::init(const JsonNode & input)
if (base)
JsonUtils::inherit(entry.second, *base);
auto * tmpl = new ObjectTemplate;
auto tmpl = std::make_shared<ObjectTemplate>();
tmpl->id = Obj(type);
tmpl->subid = subtype;
tmpl->stringID = entry.first; // FIXME: create "fullID" - type.object.template?
tmpl->readJson(entry.second);
templates.push_back(std::shared_ptr<const ObjectTemplate>(tmpl));
templates.push_back(tmpl);
}
for(const JsonNode & node : input["sounds"]["ambient"].Vector())
@ -188,12 +178,13 @@ void AObjectTypeHandler::addTemplate(JsonNode config)
config.setType(JsonNode::JsonType::DATA_STRUCT); // ensure that input is not null
if (base)
JsonUtils::inherit(config, *base);
auto * tmpl = new ObjectTemplate;
auto tmpl = std::make_shared<ObjectTemplate>();
tmpl->id = Obj(type);
tmpl->subid = subtype;
tmpl->stringID.clear(); // TODO?
tmpl->readJson(config);
templates.emplace_back(tmpl);
templates.push_back(tmpl);
}
std::vector<std::shared_ptr<const ObjectTemplate>> AObjectTypeHandler::getTemplates() const

View File

@ -109,13 +109,13 @@ std::vector<JsonNode> CObjectClassesHandler::loadLegacyData()
for (size_t i = 0; i < totalNumber; i++)
{
auto * tmpl = new ObjectTemplate;
auto tmpl = std::make_shared<ObjectTemplate>();
tmpl->readTxt(parser);
parser.endLine();
std::pair key(tmpl->id, tmpl->subid);
legacyTemplates.insert(std::make_pair(key, std::shared_ptr<const ObjectTemplate>(tmpl)));
legacyTemplates.insert(std::make_pair(key, tmpl));
}
objects.resize(256);

View File

@ -35,17 +35,17 @@ void DwellingInstanceConstructor::initTypeData(const JsonNode & input)
const auto totalLevels = levels.size();
availableCreatures.resize(totalLevels);
for(auto currentLevel = 0; currentLevel < totalLevels; currentLevel++)
for(int currentLevel = 0; currentLevel < totalLevels; currentLevel++)
{
const JsonVector & creaturesOnLevel = levels[currentLevel].Vector();
const auto creaturesNumber = creaturesOnLevel.size();
availableCreatures[currentLevel].resize(creaturesNumber);
for(auto currentCreature = 0; currentCreature < creaturesNumber; currentCreature++)
for(int currentCreature = 0; currentCreature < creaturesNumber; currentCreature++)
{
VLC->identifiers()->requestIdentifier("creature", creaturesOnLevel[currentCreature], [=] (si32 index)
VLC->identifiers()->requestIdentifier("creature", creaturesOnLevel[currentCreature], [this, currentLevel, currentCreature] (si32 index)
{
availableCreatures[currentLevel][currentCreature] = VLC->creh->objects[index];
availableCreatures.at(currentLevel).at(currentCreature) = VLC->creh->objects[index];
});
}
assert(!availableCreatures[currentLevel].empty());

View File

@ -1074,14 +1074,14 @@ void CMapLoaderJson::MapObjectLoader::construct()
auto handler = VLC->objtypeh->getHandlerFor( ModScope::scopeMap(), typeName, subtypeName);
auto * appearance = new ObjectTemplate;
auto appearance = std::make_shared<ObjectTemplate>();
appearance->id = Obj(handler->getIndex());
appearance->subid = handler->getSubIndex();
appearance->readJson(configuration["template"], false);
// Will be destroyed soon and replaced with shared template
instance = handler->create(std::shared_ptr<const ObjectTemplate>(appearance));
instance = handler->create(appearance);
instance->id = ObjectInstanceID(static_cast<si32>(owner->map->objects.size()));
instance->instanceName = jsonKey;