Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CCNotificationCenter::removeAllObservers(CCObject *target) #2134

Merged
merged 1 commit into from Mar 11, 2013
Merged

Added CCNotificationCenter::removeAllObservers(CCObject *target) #2134

merged 1 commit into from Mar 11, 2013

Conversation

AsadR
Copy link
Contributor

@AsadR AsadR commented Mar 10, 2013

Sometimes it's more convenient to use this call in destructors so that all observers associated with a particular target are removed. This prevents crashes in case a developer forgets to remove all observers for their object before it is destroyed.

This is a helper method that removes all observers with a particular
target. It can be used in destuctors to ensure no observers trigger
callbacks on objects that no longer exist.
}

void NotificationCenterTest::toExtensionsMainLayer(cocos2d::CCObject* sender)
{
/* for testing removeAllObservers */
int numObserversRemoved = CCNotificationCenter::sharedNotificationCenter()->removeAllObservers(this);
CCAssert(numObserversRemoved >= 3, "All observers were not removed!");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"All observers were not removed!" ~> "All observers were removed!"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assert will trigger when numObserversRemoved < 3, which means there are still observers leftover and not all of them were removed. In that situation the debug message should say why the assert triggered, so the reason we give for the assert trigger is: "All observers were not removed!".

When all the observers are successfully removed the assertion does not trigger.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense. Thank you.

dumganhar pushed a commit that referenced this pull request Mar 11, 2013
fixed #1822: Added CCNotificationCenter::removeAllObservers(CCObject *target).
@dumganhar dumganhar merged commit a0e87c4 into cocos2d:master Mar 11, 2013
@AsadR AsadR deleted the remove-all-notifications-for-target branch March 11, 2013 05:59
angeltown pushed a commit to angeltown/cocos2d-x that referenced this pull request Apr 28, 2014
…or-target

fixed cocos2d#1822: Added CCNotificationCenter::removeAllObservers(CCObject *target).
angeltown pushed a commit to angeltown/cocos2d-x that referenced this pull request Apr 29, 2014
…or-target

fixed cocos2d#1822: Added CCNotificationCenter::removeAllObservers(CCObject *target).
dumganhar pushed a commit that referenced this pull request May 4, 2014
fixed #1822: Added CCNotificationCenter::removeAllObservers(CCObject *target).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants