I'm trying to make a very simple lua wrapper that can be used to load & run multiple Lua scripts. I'm concerned because I don't see any documentation on how to properly destroy/delete loaded scripts without completely destroying the lua_State itself.
Is it possible to delete/unload loaded lua scripts? Is this unnecessary or will continuously calling luaL_dofile lead to a memory leak?
Simplified question.... If I call luaL_dofile on the same lua_State object, will this lead to a memory leak or issues or does lua handle this in the back end as it loads a new script?
Here's a demo...
lua_State* m_lua_state = luaL_newstate();
lua_gc(m_lua_state, LUA_GCSTOP, 0);
luaL_openlibs(m_lua_state);
lua_gc(m_lua_state, LUA_GCRESTART, 0);
for(int i = 0; i < 99999999; i++)
{
// Since I don't unload the previous file, does this cause a memory leak until lua_close is called?
if (luaL_dofile(m_lua_state, file_path.c_str()) != LUA_OK)
{
std::string error_msg = lua_tostring(m_lua_state, -1);
std::cout << "Error: " << error_msg << std::endl;
return false;
}
else
{
lua_getglobal(m_lua_state, function_name.c_str());
if (lua_isfunction(m_lua_state, -1))
{
int stack_size = lua_gettop(m_lua_state);
int number_of_args = 0;
if (lua_pcall(m_lua_state, number_of_args, 0, 0) != LUA_OK)
{
std::cout << "Error Calling Function In Script: " << file_path << "::" << function_name << " - " << lua_tostring(m_lua_state, -1) << std::endl;
}
int total_return_values = lua_gettop(m_lua_state) - stack_size;
}
else
{
std::cout << "Error Invalid Function In Script: " << file_path << "::" << function_name << std::endl;
}
}
}
lua_close(m_lua_state);