From 4d2c26871061541989aef7908767b9a0bbd34001 Mon Sep 17 00:00:00 2001 From: "andrey.bychkov" Date: Mon, 10 Aug 2026 16:26:13 +0300 Subject: [PATCH] fix(piluaprogram): add destructor to close Lua state luaL_newstate() allocates a Lua state (~2-4 MB with libraries) in the constructor, but there was no destructor to call lua_close(). Every PILuaProgram instance leaked its entire Lua state. Add ~PILuaProgram() that calls lua_close(PRIVATE->lua_state). --- libs/lua/piluaprogram.cpp | 5 +++++ libs/main/lua/piluaprogram.h | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/libs/lua/piluaprogram.cpp b/libs/lua/piluaprogram.cpp index 2a09518b..3c4bca65 100644 --- a/libs/lua/piluaprogram.cpp +++ b/libs/lua/piluaprogram.cpp @@ -30,6 +30,11 @@ PILuaProgram::PILuaProgram() { } +PILuaProgram::~PILuaProgram() { + lua_close(PRIVATE->lua_state); +} + + bool PILuaProgram::load(const PIString & script) { int ret = luaL_dostring(PRIVATE->lua_state, script.dataUTF8()); if (ret != 0) return false; diff --git a/libs/main/lua/piluaprogram.h b/libs/main/lua/piluaprogram.h index 73a863ed..2e7b95cf 100644 --- a/libs/main/lua/piluaprogram.h +++ b/libs/main/lua/piluaprogram.h @@ -41,6 +41,10 @@ public: //! \~russian Создает объект Lua-программы и открывает стандартные библиотеки Lua. PILuaProgram(); + //! \~english Closes the Lua state and releases all associated resources. + //! \~russian Закрывает Lua state и освобождает все связанные ресурсы. + ~PILuaProgram(); + //! \~english Loads and executes Lua source code from \a script. //! \~russian Загружает и выполняет исходный код Lua из \a script. bool load(const PIString & script);