lua-users home
lua-l archive

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

If you don't keep any reference to your thread in Lua storage space, the thread will be collected at next gc cycle. So if you keep a pointer to it in C and try to resume it later, it is probably the cause of your crash.

If you want to keep a reference to your thread in Lua storage space you can put it in the registry with the pointer as a key. It has been treated many times on the list already. Give us a snippet of your thread creation and destruction code so that someone can show you how to fix that issue.

Below is the simplest test I could concoct to repro. There are a few interesting cases:

1.  if (A) is called (dostring) instead of (B), all is good.
2. if (B) is called with (C) commented out, lua dies (after about 50 iterations) 3. if (C) is then reinstated, we still go 50 iterations, and then it reports that test() is undefined. 4. if the line: lua_gc( m_masterLuaState, LUA_GCSTOP, 0 ); is added just after lua_open, we still crash - this would seem to indicate the crash is not GC-related, or that I don't understand the purpose of lua_gc.

Thanks for any help!

void doThreadString( lua_State *master, const char *string )
	// creates a new thread from the master lua state and executes the string
	lua_State * pThread = lua_newthread( master );
	// prevent thread from being garbage collected	(C)
	//lua_pushvalue( master, -1 );
	//int threadRef = luaL_ref( master, LUA_REGISTRYINDEX );
	int status = luaL_loadbuffer( pThread, string, strlen(string), "LuaTest");
	if (status == 0)
		status = lua_resume( pThread, 0 );
	if (status!=0)
		// view error message on stack...
	//luaL_unref( master, LUA_REGISTRYINDEX, threadRef );  // (C)

void main ()
	lua_State *master = lua_open();
	luaL_dostring(master,"function test() end");

	// call Lua repeatedly.
	for(int i=1; i<10000; i++)
		//luaL_dostring(master,"test()");	// no problems : (A)
		doThreadString(master,"test()");	// crashes ~ 50 iterations : (B)
	lua_close( master );