Problems of current mechanism
Current cocos2d-x control the lua userdata referenceCount and the pointer value stored in the userdata by the c++. In this mechanism, the Lua scripting developers didn’t know when the pointer ptr stored in the userdata would be assigned the null value.So,many Lua script developer will happend the error that "invalid ‘cobj’ in function ‘function name’ during they develop games.
A Error that many Lua script developer would happen as follows:
--create a Sprite local sprite = cc.Sprite:create() --Use this sprite in the scheduler update function local funtion update(dt) --may trigger error, error: "invalid 'cobj' in function 'lua_cocos2dx_Sprite_setFlippedY'" sprite:setFlippedY(true) end
In current mechanism, the sprite is a autorelese object, and the up codes didn’t call
retain, so sprite was released and trigger the destrutor in the c++.It will clear the pointer value stored in the related userdata,so when the related userdata call the function bound to lua,it didn’t found the related pointer value,then cause this error.
Solution for this problem
Control the life cycle of c++ object by the lua.
The advantages of solution
- It more accord with the concept of the Lua developers, they don’t need to care about when the related c++ pointer assigned null
The disadvantages of solution
- This will lead to the delay collection of the c++ object and then leading to memory rises.
- The delay collection of the c++ objecte will break mechanism of the cocos2d-x, and lead to some unpredictable errors.
- Some bindings functions will implement retain operation for the incoming parameters, in this situation the related userdata in the Lua also needs to implement to the incomine userdata. But these functions do not have any rule to locate, so may be some operataions will be missed.
So guys,can you tell me which mechanism is better and give me some suggestions,thanks.