CCControlButton crashes when calling setBackgroundSpriteForState

CCControlButton crashes when calling setBackgroundSpriteForState
0.0 0


Hi folks,

I’ve discovered what I think is a problem with the CCControlButton implementation. When you try to update the background sprite it dies when attempting to reset the layout. For example:

CCControlButton *button = CCControlButton::create(...); //... CCScale9Sprite *sprite = CCScale9Sprite::create("newBackground.png"); myButton->setBackgroundSpriteForState(sprite, CCControlStateNormal);

^ that causes a crash when CCControlButton::needsLayout() is called. It looks like the member m_backgroundSprite is not getting set as expected, even though this line looks like it’s attempting to do so:
m_backgroundSpriteDispatchTable->setObject(sprite, state);

Anybody else having this issue?




I discovered this problem too. I think the problem is m_backgroundSprite not retain in CCControlButton when assigning.


This is still an issue in cocos2d-2.0-x-2.0.2. I’ve found the fix is to add “m_backgroundSprite = sprite;” to line 462 of CCControlButton.cpp.



`BenWard Thanks for this post, but setting “m_backgroundSprite = sprite” in that line does not look right.

You have to make changes in CCControlButton.h / CCControlButton.cpp

`CC_SYNTHESIZE_RETAIN(CCScale9Sprite**, m_backgroundSprite, BackgroundSprite);
// CCScale9Sprite** m_backgroundSprite;@

and in the implementation all calls for

m_backgroundSprite = …. -> setBackgroundSprite(…)

and at CCControlButton::~CCControlButton()