From 0a9c3a2614ce97cba42788fc5432c4f36a9216b5 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 2 Oct 2023 01:32:49 -0400 Subject: [PATCH] Fixed memory leak in wren scriptign engine --- Nuake/src/Scripting/ScriptingEngine.cpp | 45 +++++++++++++------------ 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/Nuake/src/Scripting/ScriptingEngine.cpp b/Nuake/src/Scripting/ScriptingEngine.cpp index 70a1557b..9b2df383 100644 --- a/Nuake/src/Scripting/ScriptingEngine.cpp +++ b/Nuake/src/Scripting/ScriptingEngine.cpp @@ -19,20 +19,20 @@ namespace Nuake { std::vector ScriptingEngine::m_LoadedScripts; void errorFn(WrenVM* vm, WrenErrorType errorType, - const char* module, const int line, + const char* moduleName, const int line, const char* msg) { switch (errorType) { case WREN_ERROR_COMPILE: { - std::string t = std::string(module) + " line " + std::to_string(line) + ": " + msg; + std::string t = std::string(moduleName) + " line " + std::to_string(line) + ": " + msg; Logger::Log(t, "script", CRITICAL); Engine::ExitPlayMode(); } break; case WREN_ERROR_STACK_TRACE: { - std::string t = "Stack trace: " + std::string(module) + " line " + std::to_string(line) + ": " + msg; + std::string t = "Stack trace: " + std::string(moduleName) + " line " + std::to_string(line) + ": " + msg; Logger::Log(t, "script", CRITICAL); } break; case WREN_ERROR_RUNTIME: @@ -70,20 +70,26 @@ namespace Nuake { std::map _ModulesSRC = { - {"Audio", GET_STATIC_RESOURCE_SCRIPT_SRC(StaticResources::Resources_Scripts_Audio_wren)}, - {"Engine", GET_STATIC_RESOURCE_SCRIPT_SRC(StaticResources::Resources_Scripts_Engine_wren)}, - {"Input", GET_STATIC_RESOURCE_SCRIPT_SRC(StaticResources::Resources_Scripts_Input_wren)}, - {"Math", GET_STATIC_RESOURCE_SCRIPT_SRC(StaticResources::Resources_Scripts_Math_wren)}, - {"Physics", GET_STATIC_RESOURCE_SCRIPT_SRC(StaticResources::Resources_Scripts_Physics_wren)}, - {"Scene", GET_STATIC_RESOURCE_SCRIPT_SRC(StaticResources::Resources_Scripts_Scene_wren)}, - {"ScriptableEntity", GET_STATIC_RESOURCE_SCRIPT_SRC(StaticResources::Resources_Scripts_ScriptableEntity_wren)}, + { "Audio", GET_STATIC_RESOURCE_SCRIPT_SRC(StaticResources::Resources_Scripts_Audio_wren) }, + { "Engine", GET_STATIC_RESOURCE_SCRIPT_SRC(StaticResources::Resources_Scripts_Engine_wren) }, + { "Input", GET_STATIC_RESOURCE_SCRIPT_SRC(StaticResources::Resources_Scripts_Input_wren) }, + { "Math", GET_STATIC_RESOURCE_SCRIPT_SRC(StaticResources::Resources_Scripts_Math_wren) }, + { "Physics", GET_STATIC_RESOURCE_SCRIPT_SRC(StaticResources::Resources_Scripts_Physics_wren) }, + { "Scene", GET_STATIC_RESOURCE_SCRIPT_SRC(StaticResources::Resources_Scripts_Scene_wren) }, + { "ScriptableEntity", GET_STATIC_RESOURCE_SCRIPT_SRC(StaticResources::Resources_Scripts_ScriptableEntity_wren) }, }; + void onCompleteCB(WrenVM* vm, const char* name, WrenLoadModuleResult result) + { + delete result.source; + } + const std::string NuakeModulePrefix = "Nuake:"; WrenLoadModuleResult myLoadModule(WrenVM* vm, const char* name) { WrenLoadModuleResult result = { 0 }; + result.onComplete = &onCompleteCB; // This will take care of the clean up of the new char* std::string sname = std::string(name); std::string path = "resources/" + std::string(name); @@ -118,7 +124,9 @@ namespace Nuake { fileContent = FileSystem::ReadFile(path, true); } - char* c = strcpy(new char[fileContent.length() + 1], fileContent.c_str()); + // We have to use c style char array and the callback takes care of cleaning up. + char* sourceC = new char[fileContent.length() + 1]; + char* c = strcpy(sourceC, fileContent.c_str()); // copy into c array result.source = c; return result; @@ -193,16 +201,11 @@ namespace Nuake { m_WrenVM = wrenNewVM(&config); Logger::Log("Registing Scripting Modules"); - Ref engineModule = CreateRef(); - RegisterModule(engineModule); - Ref sceneModule = CreateRef(); - RegisterModule(sceneModule); - Ref mathModule = CreateRef(); - RegisterModule(mathModule); - Ref inputModule = CreateRef(); - RegisterModule(inputModule); - Ref physicsModule = CreateRef(); - RegisterModule(physicsModule); + RegisterModule(CreateRef()); + RegisterModule(CreateRef()); + RegisterModule(CreateRef()); + RegisterModule(CreateRef()); + RegisterModule(CreateRef()); Logger::Log("Scripting Modules Registered"); Logger::Log("Scripting Engine initialized successfully"); }