A hidden danger of tolua ++ in cocos2d-x LUA 2.x
If it is the first time in the cocos2d-x to use lua, the extern inside. lua will bring a lot of help, because it helps lua, such barely object-based language, implement the inheritance function.
At the same time, the cocos2d-x integrates tolua ++, so that you can write your own c ++ class through the configuration, easily generate lua binding code.
However, not everyone will thoroughly study tolua ++, and not necessarily understand the use of tolua ++.
At the beginning of the project, we found that the tolua ++ release mechanism was not complete. For example, the following code:
tolua_pushusertype(tolua_S,tolua_obj,"CCSize"); tolua_register_gc(tolua_S,lua_gettop(tolua_S));
Although it seems that the recycling mechanism is registered on the surface, in fact, it will not be recycled immediately, but it will be recycled when it reaches a certain peak, which will cause the game to get stuck at a certain time point.
Do not talk about the problem in the future, because the solution is based on the actual scenario. During this time, we found a more serious problem, because we modeled it on basic. lua, and a baseGameLib. lua is used to bind the self-written c ++ code to lua. As the project progresses, we write more and more new c ++ code and the logic becomes more and more complex, we have even extended extern. the class mechanism in lua saves the inheritance tree and adds a syntax similar to isInstance in java. Finally, the game broke out with the combination of several factors. During the operation, the game suddenly experienced the problem that A inherited B, but B inherited. After checking for more than a week, I finally found the problem.
At the top of the basic. lua file, there is the following code:
local CCObjectTypes = { "CCObject", "CCAction", "CCImage", "CCFiniteTimeAction", ....... "}-- register CCObject typesfor i = 1, #CCObjectTypes do _push_functions[CCObjectTypes[i]] = "toluafix_pushusertype_ccobject"end
The purpose of this Code is to specify that the c ++ classes inherit CCObject. Once configured here, the class will be used when tolua ++ is generated.
Toluafix_pushusertype_ccobject to return to lua. Otherwise, all c ++ classes not configured here will be used.
Toluafix_pushusertype.
The code for toluafix_pushusertype is as follows:
void tolua_pushusertype_internal (lua_State* L, void* value, const char* type, int addToRoot){ if (value == NULL) lua_pushnil(L); else { luaL_getmetatable(L, type); /* stack: mt */ if (lua_isnil(L, -1)) { /* NOT FOUND metatable */ lua_pop(L, 1); return; } lua_pushstring(L,"tolua_ubox"); lua_rawget(L,-2); /* stack: mt ubox */ if (lua_isnil(L, -1)) { lua_pop(L, 1); lua_pushstring(L, "tolua_ubox"); lua_rawget(L, LUA_REGISTRYINDEX); }; lua_pushlightuserdata(L,value); /* stack: mt ubox key
*/ lua_rawget(L,-2); /* stack: mt ubox ubox[value] */ if (lua_isnil(L,-1)) { lua_pop(L,1); /* stack: mt ubox */ lua_pushlightuserdata(L,value); *(void**)lua_newuserdata(L,sizeof(void *)) = value; /* stack: mt ubox value newud */ lua_pushvalue(L,-1); /* stack: mt ubox value newud newud */ lua_insert(L,-4); /* stack: mt newud ubox value newud */ lua_rawset(L,-3); /* ubox[value] = newud, stack: mt newud ubox */ lua_pop(L,1); /* stack: mt newud */ /*luaL_getmetatable(L,type);*/ lua_pushvalue(L, -2); /* stack: mt newud mt */ lua_setmetatable(L,-2); /* update mt, stack: mt newud */ #ifdef LUA_VERSION_NUM lua_pushvalue(L, TOLUA_NOPEER); /* stack: mt newud peer */ lua_setfenv(L, -2); /* stack: mt newud */#endif } else { /* check the need of updating the metatable to a more specialized class */ lua_insert(L,-2); /* stack: mt ubox[u] ubox */ lua_pop(L,1); /* stack: mt ubox[u] */ lua_pushstring(L,"tolua_super"); lua_rawget(L,LUA_REGISTRYINDEX); /* stack: mt ubox[u] super */ lua_getmetatable(L,-2); /* stack: mt ubox[u] super mt */ lua_rawget(L,-2); /* stack: mt ubox[u] super super[mt] */ if (lua_istable(L,-1)) { lua_pushstring(L,type); /* stack: mt ubox[u] super super[mt] type */ lua_rawget(L,-2); /* stack: mt ubox[u] super super[mt] flag */ if (lua_toboolean(L,-1) == 1) /* if true */ { lua_pop(L,3); /* mt ubox[u]*/ lua_remove(L, -2); return; } } /* type represents a more specilized type */ /*luaL_getmetatable(L,type); // stack: mt ubox[u] super super[mt] flag mt */ lua_pushvalue(L, -5); /* stack: mt ubox[u] super super[mt] flag mt */ lua_setmetatable(L,-5); /* stack: mt ubox[u] super super[mt] flag */ lua_pop(L,3); /* stack: mt ubox[u] */ } lua_remove(L, -2); /* stack: ubox[u]*/ if (0 != addToRoot) { lua_pushvalue(L, -1); tolua_add_value_to_root(L, value); } }}
The code for the toluafix_pushusertype_ccobject method is as follows:
TOLUA_API int toluafix_pushusertype_ccobject(lua_State* L, int refid, int* p_refid, void* ptr, const char* type){ if (ptr == NULL || p_refid == NULL) { lua_pushnil(L); return -1; } if (*p_refid == 0) { *p_refid = refid; lua_pushstring(L, TOLUA_REFID_PTR_MAPPING); lua_rawget(L, LUA_REGISTRYINDEX); /* stack: refid_ptr */ lua_pushinteger(L, refid); /* stack: refid_ptr refid */ lua_pushlightuserdata(L, ptr); /* stack: refid_ptr refid ptr */ lua_rawset(L, -3); /* refid_ptr[refid] = ptr, stack: refid_ptr */ lua_pop(L, 1); /* stack: - */ lua_pushstring(L, TOLUA_REFID_TYPE_MAPPING); lua_rawget(L, LUA_REGISTRYINDEX); /* stack: refid_type */ lua_pushinteger(L, refid); /* stack: refid_type refid */ lua_pushstring(L, type); /* stack: refid_type refid type */ lua_rawset(L, -3); /* refid_type[refid] = type, stack: refid_type */ lua_pop(L, 1); /* stack: - */ //printf("[LUA] push CCObject OK - refid: %d, ptr: %x, type: %s\n", *p_refid, (int)ptr, type); } tolua_pushusertype_and_addtoroot(L, ptr, type); return 0;}
In layman's terms,
Toluafix_pushusertype has potential risks, as I just mentioned. In combination with various complex conditions, for example, when a timer is enabled for a piece of code, the objects in the timer logic have a complex inheritance tree and use the isInstance function, or there are other conditions that I cannot understand, even if you create an object in c ++ and bind it to lua, the if (lua_isnil (L,-1) code returns false, directly causing an error.
So the authors of cocos2d-x Cleverly invented toluafix_pushusertype_ccobject as long as we fully follow basic. like lua, the newly created classes that inherit CCObject are configured in basic. in lua, there will be no problems I mentioned above.