From dd6fed7928f010231e66c8a0450f9d0259524164 Mon Sep 17 00:00:00 2001 From: Aaron Date: Wed, 15 Jun 2016 07:55:29 +0100 Subject: [PATCH] Fixed ClippingNode not cleaning up _stencil correctly (#15814) * Fixed ClippingNode not cleaning up _stencil correctly _stencil may be running due to onEnter() called by ClippingNode. If onExit() is not called then Node::~Node() will assert when _stencil is released. * Added onEnterXXX to ClippingNode::setStencil() ClippingNode::_stencil would not be initialised correctly when set after ClippingNode was adding to the active scene. --- cocos/2d/CCClippingNode.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/cocos/2d/CCClippingNode.cpp b/cocos/2d/CCClippingNode.cpp index d7e4b7fa25..c099517122 100644 --- a/cocos/2d/CCClippingNode.cpp +++ b/cocos/2d/CCClippingNode.cpp @@ -290,6 +290,7 @@ Node* ClippingNode::getStencil() const void ClippingNode::setStencil(Node *stencil) { + //early out if the stencil is already set if (_stencil == stencil) return; @@ -303,9 +304,26 @@ void ClippingNode::setStencil(Node *stencil) sEngine->retainScriptObject(this, stencil); } #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS - CC_SAFE_RETAIN(stencil); - CC_SAFE_RELEASE(_stencil); + + //cleanup current stencil + if(_stencil != nullptr && _stencil->isRunning()) + { + _stencil->onExitTransitionDidStart(); + _stencil->onExit(); + } + CC_SAFE_RELEASE_NULL(_stencil); + + //initialise new stencil _stencil = stencil; + CC_SAFE_RETAIN(_stencil); + if(_stencil != nullptr && this->isRunning()) + { + _stencil->onEnter(); + if(this->_isTransitionFinished) + { + _stencil->onEnterTransitionDidFinish(); + } + } } bool ClippingNode::hasContent() const