From f8289a5d78346e7ff569f4993e97db58ce55592f Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 30 Apr 2024 02:49:12 +0300 Subject: [PATCH] leaks fix (valgrind full leak check) --- src/engine.cpp | 2 ++ src/graphics/core/Shader.cpp | 1 + src/logic/scripting/lua/LuaState.cpp | 1 + src/world/WorldGenerators.cpp | 24 ++++++++---------------- src/world/WorldGenerators.h | 8 +++++--- 5 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index bc7a619e..358c2fbc 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -172,8 +172,10 @@ Engine::~Engine() { content.reset(); assets.reset(); gui.reset(); + logger.info() << "gui finished"; audio::close(); scripting::close(); + logger.info() << "scripting finished"; Window::terminate(); logger.info() << "engine finished"; } diff --git a/src/graphics/core/Shader.cpp b/src/graphics/core/Shader.cpp index 52ecacd1..10531c34 100644 --- a/src/graphics/core/Shader.cpp +++ b/src/graphics/core/Shader.cpp @@ -73,6 +73,7 @@ void Shader::uniform3f(const std::string& name, glm::vec3 xyz){ inline auto shader_deleter = [](GLuint* shader) { glDeleteShader(*shader); + delete shader; }; inline const uint GL_LOG_LEN = 512; diff --git a/src/logic/scripting/lua/LuaState.cpp b/src/logic/scripting/lua/LuaState.cpp index 9014e89b..6e534773 100644 --- a/src/logic/scripting/lua/LuaState.cpp +++ b/src/logic/scripting/lua/LuaState.cpp @@ -396,6 +396,7 @@ void lua::LuaState::removeEnvironment(int id) { } lua_pushnil(L); setglobal(envName(id)); + logger.info() << "removed environment " << envName(id); } void lua::LuaState::dumpStack() { diff --git a/src/world/WorldGenerators.cpp b/src/world/WorldGenerators.cpp index 54570e76..78e7bc0e 100644 --- a/src/world/WorldGenerators.cpp +++ b/src/world/WorldGenerators.cpp @@ -2,18 +2,13 @@ #include "../voxels/WorldGenerator.h" #include "../voxels/FlatWorldGenerator.h" #include "../content/Content.h" -#include -#include -#include #include std::vector WorldGenerators::getGeneratorsIDs() { std::vector ids; - - for(std::map::iterator it = generators.begin(); it != generators.end(); ++it) { - ids.push_back(it->first); + for (auto& entry : generators) { + ids.push_back(entry.first); } - return ids; } @@ -21,13 +16,10 @@ std::string WorldGenerators::getDefaultGeneratorID() { return "core:default"; } -WorldGenerator* WorldGenerators::createGenerator(std::string id, const Content* content) { - for(std::map::iterator it = generators.begin(); it != generators.end(); ++it) { - if(id == it->first) { - return (WorldGenerator*) it->second(content); - } +std::unique_ptr WorldGenerators::createGenerator(std::string id, const Content* content) { + auto found = generators.find(id); + if (found == generators.end()) { + throw std::runtime_error("unknown generator id: "+id); } - - std::cerr << "unknown generator id: " << id << std::endl; - return nullptr; -} \ No newline at end of file + return std::unique_ptr(found->second(content)); +} diff --git a/src/world/WorldGenerators.h b/src/world/WorldGenerators.h index 596debb0..01940394 100644 --- a/src/world/WorldGenerators.h +++ b/src/world/WorldGenerators.h @@ -11,7 +11,7 @@ typedef WorldGenerator* (*gen_constructor) (const Content*); class WorldGenerators { - static inline std::map generators = *(new std::map); + static inline std::map generators; public: template @@ -21,7 +21,9 @@ public: static std::string getDefaultGeneratorID(); - static WorldGenerator* createGenerator(std::string id, const Content* content); + static std::unique_ptr createGenerator( + std::string id, const Content* content + ); }; template @@ -33,4 +35,4 @@ void WorldGenerators::addGenerator(std::string id) { }; } -#endif /* WORLD_WORLDGENERATORS_H_ */ \ No newline at end of file +#endif /* WORLD_WORLDGENERATORS_H_ */