From d4f65b53484ba3b2b36b2be8dff9df7a1b4fefd8 Mon Sep 17 00:00:00 2001 From: Denis Mingulov Date: Sun, 21 Oct 2012 11:13:57 +0300 Subject: [PATCH] CCMenu - prevent a crash if some child item is not CCMenuItem 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. --- cocos2dx/menu_nodes/CCMenu.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cocos2dx/menu_nodes/CCMenu.cpp b/cocos2dx/menu_nodes/CCMenu.cpp index 78a745bfd1..02e2672bbb 100644 --- a/cocos2dx/menu_nodes/CCMenu.cpp +++ b/cocos2dx/menu_nodes/CCMenu.cpp @@ -652,16 +652,16 @@ CCMenuItem* CCMenu::itemForTouch(CCTouch *touch) CCObject* pObject = NULL; CCARRAY_FOREACH(m_pChildren, pObject) { - CCNode* pChild = dynamic_cast(pObject); - if (pChild && pChild->isVisible() && ((CCMenuItem*)pChild)->isEnabled()) + CCMenuItem* pChild = dynamic_cast(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; } } }