From a24318eedc44af2f5967c4727b7ebf578b8e233a Mon Sep 17 00:00:00 2001 From: ModMaker101 <119018978+ModMaker101@users.noreply.github.com> Date: Wed, 25 Mar 2026 00:25:18 -0400 Subject: Memory leak fix: Make chunks unload properly (#1406) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix chunk unload and cleanup logic, fixes #1347 * Applying formatting to code I edited 😝 --- Minecraft.Client/ServerChunkCache.cpp | 57 ++++++++++++----------------------- 1 file changed, 19 insertions(+), 38 deletions(-) (limited to 'Minecraft.Client/ServerChunkCache.cpp') diff --git a/Minecraft.Client/ServerChunkCache.cpp b/Minecraft.Client/ServerChunkCache.cpp index c7d70c7d..54312ffa 100644 --- a/Minecraft.Client/ServerChunkCache.cpp +++ b/Minecraft.Client/ServerChunkCache.cpp @@ -80,54 +80,31 @@ vector *ServerChunkCache::getLoadedChunkList() return &m_loadedChunkList; } -void ServerChunkCache::drop(int x, int z) +void ServerChunkCache::drop(const int x, const int z) { - // 4J - we're not dropping things anymore now that we have a fixed sized cache -#ifdef _LARGE_WORLDS - - bool canDrop = false; -// if (level->dimension->mayRespawn()) -// { -// Pos *spawnPos = level->getSharedSpawnPos(); -// int xd = x * 16 + 8 - spawnPos->x; -// int zd = z * 16 + 8 - spawnPos->z; -// delete spawnPos; -// int r = 128; -// if (xd < -r || xd > r || zd < -r || zd > r) -// { -// canDrop = true; -//} -// } -// else - { - canDrop = true; - } - if(canDrop) + const int ix = x + XZOFFSET; + const int iz = z + XZOFFSET; + if ((ix < 0) || (ix >= XZSIZE)) return; + if ((iz < 0) || (iz >= XZSIZE)) return; + const int idx = ix * XZSIZE + iz; + LevelChunk* chunk = cache[idx]; + + if (chunk != nullptr) { - int ix = x + XZOFFSET; - int iz = z + XZOFFSET; - // Check we're in range of the stored level - if( ( ix < 0 ) || ( ix >= XZSIZE ) ) return; - if( ( iz < 0 ) || ( iz >= XZSIZE ) ) return; - int idx = ix * XZSIZE + iz; - LevelChunk *chunk = cache[idx]; - - if(chunk) - { - m_toDrop.push_back(chunk); - } + const auto it = std::find(m_loadedChunkList.begin(), m_loadedChunkList.end(), chunk); + if (it != m_loadedChunkList.end()) m_loadedChunkList.erase(it); + + cache[idx] = nullptr; + chunk->loaded = false; } -#endif } void ServerChunkCache::dropAll() { -#ifdef _LARGE_WORLDS for (LevelChunk *chunk : m_loadedChunkList) { drop(chunk->x, chunk->z); -} -#endif + } } // 4J - this is the original (and virtual) interface to create @@ -957,6 +934,10 @@ bool ServerChunkCache::tick() m_unloadedCache[idx] = chunk; cache[idx] = nullptr; } + else + { + continue; + } } m_toDrop.pop_front(); } -- cgit v1.2.3