CCMenuItemSprite, using function pointer as SEL_MenuHandler

This has been driving me crazy for a week now. The problem is that I am creating a CCMenuItemSprite, using a function pointer as the SEL_MenuHandler, but when the function gets call, the object of the function is basically garbage.

So my creation looks like this:

CCMenuItemSprite::create(si0, si0, this, (SEL_MenuHandler)specialStart[0]);

si0 - a sprite I defined above
this - an object that inherits from CCObject
specialStart[ 0 ] - the first index of an array of function pointers

the function pointers has this signature:

void (Paddle::*specialStart[NUM_SPECIAL_ATTACKS])(cocos2d::CCObject*);

When I click on the menuitem, everything works as expect until I reach the function specialStart[ 0 ] points to. The object of that function (this) is garbage. The strange part is, the step about the call stack in CCMenuItem:

void CCMenuItem::activate()
{
    if (m_bIsEnabled)
    {
        if (m_pListener && m_pfnSelector)
        {
            (m_pListener->*m_pfnSelector)(this);
        }

        if (kScriptTypeNone != m_eScriptType)
        {
            CCScriptEngineManager::sharedManager()->getScriptEngine()->executeMenuItemEvent(this);
        }
    }
}

m_pListener points to the correct object with correct data, and m_pfnSelector points to the correct function and same function that specialStart[ 0 ] pointed to. But as soon as the call (m_pListener->*m_pfnSelector)(this); is stepped into, m_pListener (which is now the “this” property) is garbage and not the same as m_pListener was the step above.

Someone please help me

could you give a demo for testing?
i’m confusing on how you use the function pointer in this case.

This doesn’t seem to be an issue with function pointers after even more investigating.

I think the issue has to do with the target (CCObject) of the menu_selector. In all examples online, I see the target being a CCLayer or CCScene, and the menuhandler being a function of that CCLayer/CCScene. In my case, the target is a class I created that inherits from CCObject.

Is there any reason this should fail based on the fact that my target and selector inherit from CCObject for the sole reason of receiving a callback from a button press?

Though this does not explain why (as stated above), during the listener->selector call in CCMenuItem, the pointer is correct but on once it steps inside the selector the pointer (now the “this” pointer) contains garbage

Ok solved the problem, well I didn’t solve it but I found a solution.

I removed the inheritance from CCObject from my class. Then, when I create the menu item I used

specialIcons[0] = CCMenuItemSprite::create(si0, si0, (CCObject*)this, (SEL_MenuHandler)specialStart[0]);

Notice now I hard cast this to (CCObject*). Makes no sense why it works now but I’ll take it