CCObject lua binding troubles

I’m working from cocos2d-1.0.1-x-0.10.0
I’ve run in to some troubles when trying to call certain functions from lua, such as retain, release, retainCount etc.

I think the easiest way to show you the issue is by an example. I took the base HelloLua project and I replaced the hello.lua code with this:

print("1") local t = cocos2d.CCNode:new() print("2") t:setRotation(123) t:retainCount() t:retain() t:release()

Creating a new node works.
Setting the rotation works.

Once we get to the t:retainCount() we can start seeing some issues. If you actually did a print of the retain count returned it would be something like 15 (it might be different for you). So I looked into a little bit deeper!

I put a break point in xcode on line 4935 in LuaCocos2d.cpp.
That line contains the casting calls to get the user type from the lua object passed from lua.
Looks like this:

@ cocos2d::CCObject* self = (cocos2d::CCObject**) tolua_tousertype;@
But if you step over that line and look a little bit closer at the**self* object you’ll see some strange things.

Here is a portion of the printout in gbd of the self object:

self 'cocos2d::CCNode' * 0x781de1c cocos2d::SelectorProtocol 'cocos2d::SelectorProtocol' * 0x781de1c cocos2d::CCObject 'cocos2d::CCObject' * 0x781de20 m_nZOrder int 0 m_fVertexZ float 0 m_fRotation float 0 m_fScaleX float 123 m_fScaleY float 1

You can see that the rotation has suddenly become 0, and the ScaleX somehow got the 123 value that used to be set in the rotation. Somehow all the values have been sort of “pushed down”. This really messes with the object and can also effect functions that can be called on the object. For instance if you also set a break point on when the retain or release is called through lua you will see that it doesn’t actually even go in to the proper CCObject::retain or CCObject::release functions.

Hopefully someone can figure this out because it’s really causing me issues. Thanks!

I solved the same problem by this way.
but I am not sure this is safe.

change CCNode.h

  • class CC_DLL CCNode : public SelectorProtocol, public CCObject
  • class CC_DLL CCNode : public CCObject, public SelectorProtocol

and if you use visual studio then put below #pragma code to selector_protocol.h

virtual void selectorProtocolRelease(void) {};
};

#pragma pointers_to_members( full_generality, multiple_inheritance )

class CCNode;
typedef void (SelectorProtocol::*SEL_SCHEDULE)(ccTime);

Thanks, that actually does seem to work. It’s very strange. Also I don’t know if there are some other side effects, maybe someone else can explain if doing this is safe or not.

I’m using xcode so I didn’t do any of those selector_protocol.h changes you made. Do you think I need to do any changes?

the cause is tolua.cast is C-style cast. safe way is C++ cast.
ex> static_cast<CCNode*>(pCCObj)

and another cause is “pointer to member function” in visual studio.
see this, http://msdn.microsoft.com/en-us/library/1s6193tt.aspx

i don’t know about xcode. sorry.
if you want to figure out, searching “pointer to member function” issues in xcode/gcc.