lua-users home
lua-l archive

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


> > Sorry if this has been answered before but why can't I set 
> the 'gc' tag
> > method from inside a lua program?
> 
> Because with the current implementation, plenty of things can go wrong
> during a gc tag method (and then crash your program):
> 
> - your tag method may start a (recursive) GC cycle;
> 
> - you may resurect the object being collected;
> 
> - you can access other dead objects that may be already collected;
> 
> - there may be an error in your function;
> 
Can't I do all those things from inside a C function as well? Untested code:

-----cut-----
static int destructor(lua_State *L) { // instance
	switch (choose one thing you shouldn't do inside a gc tag method) {
		case RECURSIVE:
			create_an_instance_on_stack(L);
			lua_settop(L, -2);
			lua_setgcthreshold(L, 0);
			break;
		case RESURECT:
			lua_setglobal(L, "resurected");
			break;
		case ACCESS_DEAD:
			lua_pushstring(L,
"a_table_which_could_have_already_been_collected");
			lua_gettable(L, -2);
			break;
		case ERROR_IN_TAG_METHOD: {
			int a = 1;
			a = 1 / (a - 1);
			break;
	}
	return 0;
}
-----cut-----

> There is a good chance that this will be fixed in the next version...
That would be nice.

André de Leiradella