Skip to content

Commit

Permalink
CCMenu - prevent a crash if some child item is not CCMenuItem
Browse files Browse the repository at this point in the history
CCMenu::itemForTouch expects that all children are CCMenuItems.
But it is still possible to add other child types, for example
if CCAssert is disabled or some other addChild method is called.

In this case dynamic_cast to CCNode and later try to do
((CCMenuItem*)pChild)->isEnabled() triggers an undefined behaviour
- up to a crash or something else.

Fixed by a dynamic_cast straightforward to CCMenuItem.
  • Loading branch information
mingulov committed Oct 21, 2012
1 parent 1ce8545 commit 45b2ee4
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions cocos2dx/menu_nodes/CCMenu.cpp
Expand Up @@ -652,16 +652,16 @@ CCMenuItem* CCMenu::itemForTouch(CCTouch *touch)
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild && pChild->isVisible() && ((CCMenuItem*)pChild)->isEnabled())
CCMenuItem* pChild = dynamic_cast<CCMenuItem*>(pObject);
if (pChild && pChild->isVisible() && pChild->isEnabled())
{
CCPoint local = pChild->convertToNodeSpace(touchLocation);
CCRect r = ((CCMenuItem*)pChild)->rect();
CCRect r = pChild->rect();
r.origin = CCPointZero;

if (r.containsPoint(local))
{
return (CCMenuItem*)pChild;
return pChild;
}
}
}
Expand Down

0 comments on commit 45b2ee4

Please sign in to comment.