fix weak_ptr leak

This commit is contained in:
MihailRis
2024-12-12 02:40:59 +03:00
parent 863146cf6b
commit 7e92a16016
4 changed files with 73 additions and 39 deletions
+24 -29
View File
@@ -17,34 +17,28 @@
static debug::Logger logger("chunks-storage");
ChunksStorage::ChunksStorage(Level* level) : level(level) {
ChunksStorage::ChunksStorage(Level* level)
: level(level),
chunksMap(std::make_shared<util::WeakPtrsMap<glm::ivec2, Chunk>>()) {
}
std::shared_ptr<Chunk> ChunksStorage::fetch(int x, int z) {
std::lock_guard lock(mutex);
auto found = chunksMap.find(glm::ivec2(x, z));
if (found == chunksMap.end()) {
return nullptr;
}
auto ptr = found->second.lock();
if (ptr == nullptr) {
chunksMap.erase(found);
}
return ptr;
std::lock_guard lock(*chunksMap);
return chunksMap->fetch({x, z});
}
static void check_voxels(const ContentIndices& indices, Chunk* chunk) {
static void check_voxels(const ContentIndices& indices, Chunk& chunk) {
bool corrupted = false;
blockid_t defsCount = indices.blocks.count();
for (size_t i = 0; i < CHUNK_VOL; i++) {
blockid_t id = chunk->voxels[i].id;
if (indices.blocks.get(id) == nullptr) {
blockid_t id = chunk.voxels[i].id;
if (id >= defsCount) {
if (!corrupted) {
#ifdef NDEBUG
// release
auto logline = logger.error();
logline << "corruped blocks detected at " << i << " of chunk ";
logline << chunk->x << "x" << chunk->z;
logline << chunk.x << "x" << chunk.z;
logline << " -> " << id;
corrupted = true;
#else
@@ -52,33 +46,34 @@ static void check_voxels(const ContentIndices& indices, Chunk* chunk) {
abort();
#endif
}
chunk->voxels[i].id = BLOCK_AIR;
chunk.voxels[i].id = BLOCK_AIR;
}
}
}
std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
std::lock_guard lock(mutex);
auto found = chunksMap.find(glm::ivec2(x, z));
if (found != chunksMap.end()) {
auto chunk = found->second.lock();
if (chunk) {
return chunk;
}
if (auto ptr = chunksMap->fetch({x, z})) {
return ptr;
}
World* world = level->getWorld();
auto& regions = world->wfile.get()->getRegions();
auto chunk = std::make_shared<Chunk>(x, z);
chunksMap[glm::ivec2(chunk->x, chunk->z)] = chunk;
auto& localChunksMap = chunksMap;
auto chunk = std::shared_ptr<Chunk>(
new Chunk(x, z),
[localChunksMap, x, z](auto ptr) {
std::lock_guard lock(*localChunksMap);
localChunksMap->erase({x, z});
delete ptr;
}
);
(*chunksMap)[glm::ivec2(chunk->x, chunk->z)] = chunk;
if (auto data = regions.getVoxels(chunk->x, chunk->z)) {
const auto& indices = *level->content->getIndices();
chunk->decode(data.get());
check_voxels(indices, chunk.get());
check_voxels(indices, *chunk);
auto invs = regions.fetchInventories(chunk->x, chunk->z);
auto iterator = invs.begin();
while (iterator != invs.end()) {