From e6270e64dee01e516b46f52c3454bcb0f9fbc378 Mon Sep 17 00:00:00 2001 From: Darragh Coy Date: Tue, 2 Jul 2013 14:24:14 -0700 Subject: [PATCH] Bugfixes to the Set class. 1: If adding an object to the container then only retain the object again provided it's not already been added. The underlying std::set does not support duplicate entries. (see: www.cocos2d-x.org/boards/6/topics/10876) 2: When removing an object only CC_SAFE_RELEASE it if it was actually contained in the set. (see: http://www.cocos2d-x.org/boards/6/topics/10876) 3: Fix a bug in 'removeAllObjects' where the underlying std::set container was not being cleared. (see: https://github.com/cocos2d/cocos2d-x/issues/2283) --- cocos2dx/cocoa/CCSet.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/cocos2dx/cocoa/CCSet.cpp b/cocos2dx/cocoa/CCSet.cpp index d906283499..6ca9342b80 100644 --- a/cocos2dx/cocoa/CCSet.cpp +++ b/cocos2dx/cocoa/CCSet.cpp @@ -92,27 +92,32 @@ int Set::count(void) void Set::addObject(Object *pObject) { - CC_SAFE_RETAIN(pObject); - _set->insert(pObject); + if (_set->count(pObject) == 0) + { + CC_SAFE_RETAIN(pObject); + _set->insert(pObject); + } } void Set::removeObject(Object *pObject) { - _set->erase(pObject); - CC_SAFE_RELEASE(pObject); + if (_set->erase(pObject) > 0) + { + CC_SAFE_RELEASE(pObject); + } } void Set::removeAllObjects() { - SetIterator it; - for (it = _set->begin(); it != _set->end(); ++it) + for (SetIterator it = _set->begin(); it != _set->end(); ) { - if (! (*it)) + if (!(*it)) { break; } - + (*it)->release(); + _set->erase(it++); } }