Improve memory safety

Fixed a memory leak in LocalWorld, line 350 where a new chunk was allocated and then the pointer was overwritten immediately afterwards so the memory would never get released.
This commit is contained in:
2025-01-17 21:16:02 -05:00
parent dfb20b548e
commit 79958d395d
3 changed files with 9 additions and 20 deletions

View File

@@ -14,8 +14,7 @@ namespace CaveGame::Client
private:
float splash_timer = 1.5f;
float load_percent = 0.f;
JGL::Texture* splash; // TODO: RAII on this
//JGL::Font font; // TODO: RAII on this
JGL::Texture* splash;
std::array<JGL::RenderTarget*, 16> column_textures{};

View File

@@ -53,7 +53,7 @@ namespace CaveGame::Client {
if (!cached_chunk_sprites.contains(chunk_pos))
{
chunk->touched = false;
auto* target = new JGL::RenderTarget({Core::Chunk::ChunkSize, Core::Chunk::ChunkSize}, {0,0,0,0});
auto* target = new JGL::RenderTarget({Core::Chunk::ChunkSize, Core::Chunk::ChunkSize}, {0, 0, 0, 0});
RenderChunkTexture(chunk_pos, target, chunk);
cached_chunk_sprites.insert({chunk_pos, target});
}
@@ -63,7 +63,6 @@ namespace CaveGame::Client {
chunk->touched = false;
auto* target = cached_chunk_sprites[chunk_pos];
RenderChunkTexture(chunk_pos, target, chunk);
@@ -281,25 +280,21 @@ namespace CaveGame::Client {
JGL::J2D::DrawRenderTarget(it->second, GetChunkRealCoordinates(coords));
}
void LocalWorld::LookForChunksNeedUnloading()
{
void LocalWorld::LookForChunksNeedUnloading() {
for (auto it = loaded_chunks.begin(); it != loaded_chunks.end();)
{
const auto coords = it->first;
if (!IsChunkCellWithinViewport(coords))
{
if (!IsChunkCellWithinViewport(coords)) {
// TODO: Move off main thread.
SaveChunkToFile(coords, it->second);
//delete it->second;
delete it->second;
loaded_chunks.erase(it++);
//delete cached_chunk_sprites.at(coords);
cached_chunk_sprites.erase(coords);
} else
{
++it;
}
}
}
@@ -352,16 +347,12 @@ namespace CaveGame::Client {
// Check our generator queue for complete chunks, and pull them into our loaded chunks.
while (!ServedChunks.empty())
{
Core::Chunk* c = new Core::Chunk({0,0});
Core::Chunk* c = nullptr;
ServedChunks.front_pop(c);
loaded_chunks.emplace(c->GetChunkCell(), c);
if (std::find(chunks_in_waiting.begin(), chunks_in_waiting.end(), c->GetChunkCell()) != chunks_in_waiting.end()) {
if (std::find(chunks_in_waiting.begin(), chunks_in_waiting.end(), c->GetChunkCell()) != chunks_in_waiting.end())
chunks_in_waiting.erase(std::remove(chunks_in_waiting.begin(), chunks_in_waiting.end(), c->GetChunkCell()), chunks_in_waiting.end());
}
//generator.SecondPass(this, c);
}

View File

@@ -26,8 +26,7 @@ namespace CaveGame::Core
public:
/// The default constructor does not initialize any members.
Chunk() = default;
~Chunk() { }
~Chunk() = default;
/// Constructs a chunk with empty (air) tiles, located at the given chunk-cell.