lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi, I've recently added LUA to our game project and wrapped everything up in a couple of C++ classes. Each time a new script is needed the function below is called, creating a new thread (CScript object) with new global table. My question is: How do I ensure a thread is garbage collected once discarded?
i.e.
delete pMyScript;
CScript::~CScript(void)
{
	// what to do here?
} Thanks in advance, Jamie




CScript *CScriptManager::NewScriptFromFile(const char *pFilename) {
	lua_State *pState = NewLuaState();
CScript *pScript = new CScript(pState);
	.
	.
.
	return pScript;
}
lua_State	*CScriptManager::NewLuaState(void)
{
lua_State *pState = lua_newthread(m_pBaseLuaState); lua_newtable(pState); // new table for globals
//	this does the LUA equivalent of: setmetatable( {}, { __index = _G } )
	lua_newtable(pState);	// metatable for new globals
	lua_pushstring(pState, "__index");
	lua_pushvalue(pState, LUA_GLOBALSINDEX);
lua_settable(pState, -3); lua_setmetatable(pState, -2); lua_replace(pState, LUA_GLOBALSINDEX);
	return pState;
}