From 69a17c25eacfc9bc6a214cab95ad0edae214e2a3 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 27 Dec 2023 11:44:19 +0300 Subject: [PATCH] WorldRegions memory management update --- src/files/WorldFiles.cpp | 11 ++++------- src/files/WorldFiles.h | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index c62e1d12b..0ed46ff84 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -78,9 +78,6 @@ WorldFiles::WorldFiles(fs::path directory, const DebugSettings& settings) WorldFiles::~WorldFiles(){ delete[] compressionBuffer; - for (auto it : regions){ - delete it.second; - } regions.clear(); } @@ -88,14 +85,14 @@ WorldRegion* WorldFiles::getRegion(regionsmap& regions, int x, int z) { auto found = regions.find(glm::ivec2(x, z)); if (found == regions.end()) return nullptr; - return found->second; + return found->second.get(); } WorldRegion* WorldFiles::getOrCreateRegion(regionsmap& regions, int x, int z) { WorldRegion* region = getRegion(regions, x, z); if (region == nullptr) { region = new WorldRegion(); - regions[glm::ivec2(x, z)] = region; + regions[glm::ivec2(x, z)].reset(region); } return region; } @@ -354,8 +351,8 @@ void WorldFiles::writeRegion(int x, int z, WorldRegion* entry, fs::path folder, } void WorldFiles::writeRegions(regionsmap& regions, const fs::path& folder, int layer) { - for (auto it : regions){ - WorldRegion* region = it.second; + for (auto& it : regions){ + WorldRegion* region = it.second.get(); if (region->getChunks() == nullptr || !region->isUnsaved()) continue; glm::ivec2 key = it.first; diff --git a/src/files/WorldFiles.h b/src/files/WorldFiles.h index 7b7ce48b9..879f2d3bd 100644 --- a/src/files/WorldFiles.h +++ b/src/files/WorldFiles.h @@ -52,7 +52,7 @@ class WorldRegion { uint32_t* getSizes() const; }; -typedef std::unordered_map regionsmap; +typedef std::unordered_map> regionsmap; class WorldFiles { std::unordered_map> openRegFiles;