From a544eadab8a5039c41ed628f703ab272261e8ac1 Mon Sep 17 00:00:00 2001 From: WenhaiLin Date: Thu, 2 Apr 2015 23:27:07 +0800 Subject: [PATCH] MenuItem: Fixed crash if MenuItem::onExit is called multiple times. --- cocos/2d/CCMenuItem.cpp | 52 +++++++++-------------------------------- cocos/2d/CCMenuItem.h | 3 --- 2 files changed, 11 insertions(+), 44 deletions(-) diff --git a/cocos/2d/CCMenuItem.cpp b/cocos/2d/CCMenuItem.cpp index f2dfb426fa..a9faaa8b6e 100644 --- a/cocos/2d/CCMenuItem.cpp +++ b/cocos/2d/CCMenuItem.cpp @@ -2,7 +2,7 @@ Copyright (c) 2008-2010 Ricardo Quesada Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2011 Zynga Inc. -Copyright (c) 2013-2014 Chukong Technologies Inc. +Copyright (c) 2013-2015 Chukong Technologies Inc. http://www.cocos2d-x.org @@ -33,13 +33,6 @@ THE SOFTWARE. #include "deprecated/CCString.h" #include -#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1))) -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" -#elif _MSC_VER >= 1400 //vs 2005 or higher -#pragma warning (push) -#pragma warning (disable: 4996) -#endif - NS_CC_BEGIN static int _globalFontSize = kItemSize; @@ -65,7 +58,7 @@ MenuItem* MenuItem::create() MenuItem* MenuItem::create(Ref *target, SEL_MenuHandler selector) { MenuItem *ret = new (std::nothrow) MenuItem(); - ret->initWithTarget(target, selector); + ret->initWithCallback(std::bind(selector, target, std::placeholders::_1)); ret->autorelease(); return ret; } @@ -81,8 +74,6 @@ MenuItem* MenuItem::create( const ccMenuCallback& callback) // FIXME: deprecated bool MenuItem::initWithTarget(cocos2d::Ref *target, SEL_MenuHandler selector ) { - _target = target; - CC_SAFE_RETAIN(_target); return initWithCallback( std::bind(selector,target, std::placeholders::_1) ); } @@ -102,7 +93,6 @@ MenuItem::~MenuItem() void MenuItem::onExit() { Node::onExit(); - CC_SAFE_RELEASE(_target); } void MenuItem::selected() @@ -159,8 +149,6 @@ bool MenuItem::isSelected() const // FIXME: deprecated void MenuItem::setTarget(Ref *target, SEL_MenuHandler selector) { - _target = target; - CC_SAFE_RETAIN(_target); setCallback( std::bind( selector, target, std::placeholders::_1) ); } @@ -199,7 +187,7 @@ void MenuItemLabel::setLabel(Node* var) MenuItemLabel * MenuItemLabel::create(Node*label, Ref* target, SEL_MenuHandler selector) { MenuItemLabel *ret = new (std::nothrow) MenuItemLabel(); - ret->initWithLabel(label, target, selector); + ret->initWithLabel(label, std::bind(selector, target, std::placeholders::_1)); ret->autorelease(); return ret; } @@ -223,8 +211,6 @@ MenuItemLabel* MenuItemLabel::create(Node *label) // FIXME:: deprecated bool MenuItemLabel::initWithLabel(Node* label, Ref* target, SEL_MenuHandler selector) { - _target = target; - CC_SAFE_RETAIN(_target); return initWithLabel(label, std::bind(selector,target, std::placeholders::_1) ); } @@ -329,7 +315,7 @@ MenuItemAtlasFont * MenuItemAtlasFont::create(const std::string& value, const st MenuItemAtlasFont * MenuItemAtlasFont::create(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, Ref* target, SEL_MenuHandler selector) { MenuItemAtlasFont *ret = new (std::nothrow) MenuItemAtlasFont(); - ret->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap, target, selector); + ret->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap, std::bind(selector, target, std::placeholders::_1)); ret->autorelease(); return ret; } @@ -345,8 +331,6 @@ MenuItemAtlasFont * MenuItemAtlasFont::create(const std::string& value, const st // FIXME:: deprecated bool MenuItemAtlasFont::initWithString(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, Ref* target, SEL_MenuHandler selector) { - _target = target; - CC_SAFE_RETAIN(_target); return initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap, std::bind(selector,target, std::placeholders::_1) ); } @@ -395,7 +379,7 @@ const std::string& MenuItemFont::getFontName() MenuItemFont * MenuItemFont::create(const std::string& value, Ref* target, SEL_MenuHandler selector) { MenuItemFont *ret = new (std::nothrow) MenuItemFont(); - ret->initWithString(value, target, selector); + ret->initWithString(value, std::bind(selector, target, std::placeholders::_1)); ret->autorelease(); return ret; } @@ -431,8 +415,6 @@ bool MenuItemFont::initWithString(const std::string& value, Ref* target, SEL_Men { CCASSERT( !value.empty(), "Value length must be greater than 0"); - _target = target; - CC_SAFE_RETAIN(target); return initWithString(value, std::bind(selector,target, std::placeholders::_1) ); } @@ -552,7 +534,7 @@ MenuItemSprite * MenuItemSprite::create(Node* normalSprite, Node* selectedSprite // FIXME: deprecated MenuItemSprite * MenuItemSprite::create(Node* normalSprite, Node* selectedSprite, Ref* target, SEL_MenuHandler selector) { - return MenuItemSprite::create(normalSprite, selectedSprite, nullptr, target, selector); + return MenuItemSprite::create(normalSprite, selectedSprite, nullptr, std::bind(selector, target, std::placeholders::_1)); } MenuItemSprite * MenuItemSprite::create(Node* normalSprite, Node* selectedSprite, const ccMenuCallback& callback) @@ -564,7 +546,7 @@ MenuItemSprite * MenuItemSprite::create(Node* normalSprite, Node* selectedSprite MenuItemSprite * MenuItemSprite::create(Node *normalSprite, Node *selectedSprite, Node *disabledSprite, Ref *target, SEL_MenuHandler selector) { MenuItemSprite *ret = new (std::nothrow) MenuItemSprite(); - ret->initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, target, selector); + ret->initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, std::bind(selector, target, std::placeholders::_1)); ret->autorelease(); return ret; } @@ -580,8 +562,6 @@ MenuItemSprite * MenuItemSprite::create(Node *normalSprite, Node *selectedSprite // FIXME: deprecated bool MenuItemSprite::initWithNormalSprite(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, Ref* target, SEL_MenuHandler selector) { - _target = target; - CC_SAFE_RETAIN(_target); return initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, std::bind(selector,target, std::placeholders::_1) ); } @@ -712,7 +692,7 @@ MenuItemImage * MenuItemImage::create(const std::string& normalImage, const std: // FIXME: deprecated MenuItemImage * MenuItemImage::create(const std::string& normalImage, const std::string& selectedImage, Ref* target, SEL_MenuHandler selector) { - return MenuItemImage::create(normalImage, selectedImage, "", target, selector); + return MenuItemImage::create(normalImage, selectedImage, "", std::bind(selector, target, std::placeholders::_1)); } MenuItemImage * MenuItemImage::create(const std::string& normalImage, const std::string& selectedImage, const ccMenuCallback& callback) @@ -724,7 +704,7 @@ MenuItemImage * MenuItemImage::create(const std::string& normalImage, const std: MenuItemImage * MenuItemImage::create(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage, Ref* target, SEL_MenuHandler selector) { MenuItemImage *ret = new (std::nothrow) MenuItemImage(); - if (ret && ret->initWithNormalImage(normalImage, selectedImage, disabledImage, target, selector)) + if (ret && ret->initWithNormalImage(normalImage, selectedImage, disabledImage, std::bind(selector, target, std::placeholders::_1))) { ret->autorelease(); return ret; @@ -760,8 +740,6 @@ MenuItemImage * MenuItemImage::create(const std::string& normalImage, const std: // FIXME:: deprecated bool MenuItemImage::initWithNormalImage(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage, Ref* target, SEL_MenuHandler selector) { - _target = target; - CC_SAFE_RETAIN(_target); return initWithNormalImage(normalImage, selectedImage, disabledImage, std::bind(selector,target, std::placeholders::_1) ); } bool MenuItemImage::initWithNormalImage(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage, const ccMenuCallback& callback) @@ -813,7 +791,7 @@ void MenuItemImage::setDisabledSpriteFrame(SpriteFrame * frame) MenuItemToggle * MenuItemToggle::createWithTarget(Ref* target, SEL_MenuHandler selector, const Vector& menuItems) { MenuItemToggle *ret = new (std::nothrow) MenuItemToggle(); - ret->MenuItem::initWithTarget(target, selector); + ret->MenuItem::initWithCallback(std::bind(selector, target, std::placeholders::_1)); ret->_subItems = menuItems; ret->_selectedIndex = UINT_MAX; ret->setSelectedIndex(0); @@ -836,7 +814,7 @@ MenuItemToggle * MenuItemToggle::createWithTarget(Ref* target, SEL_MenuHandler s va_list args; va_start(args, item); MenuItemToggle *ret = new (std::nothrow) MenuItemToggle(); - ret->initWithTarget(target, selector, item, args); + ret->initWithCallback(std::bind(selector, target, std::placeholders::_1), item, args); ret->autorelease(); va_end(args); return ret; @@ -877,8 +855,6 @@ MenuItemToggle * MenuItemToggle::create() // FIXME:: deprecated bool MenuItemToggle::initWithTarget(Ref* target, SEL_MenuHandler selector, MenuItem* item, va_list args) { - _target = target; - CC_SAFE_RETAIN(_target); return initWithCallback(std::bind( selector, target, std::placeholders::_1), item, args); } @@ -995,9 +971,3 @@ MenuItem* MenuItemToggle::getSelectedItem() } NS_CC_END - -#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1))) -#pragma GCC diagnostic warning "-Wdeprecated-declarations" -#elif _MSC_VER >= 1400 //vs 2005 or higher -#pragma warning (pop) -#endif diff --git a/cocos/2d/CCMenuItem.h b/cocos/2d/CCMenuItem.h index 01bed48f11..807661def1 100644 --- a/cocos/2d/CCMenuItem.h +++ b/cocos/2d/CCMenuItem.h @@ -110,7 +110,6 @@ CC_CONSTRUCTOR_ACCESS: : _selected(false) , _enabled(false) , _callback(nullptr) - , _target(nullptr) {} /** * @js NA @@ -133,8 +132,6 @@ protected: bool _enabled; // callback ccMenuCallback _callback; - // If using the old API, the _target needs to be retained / released - Ref *_target; private: CC_DISALLOW_COPY_AND_ASSIGN(MenuItem);