From 96b84fd2c0850c52e385618ae69dbfd6e58a2c92 Mon Sep 17 00:00:00 2001 From: chenxianyin <420373550@qq.com> Date: Fri, 11 May 2018 09:49:22 +0800 Subject: [PATCH 1/2] when a null spriteFrame was set, the old sriteFrame must be free. (#1225) * when a null spriteFrame was set, the old sriteFrame must be free. * when Scale9SpriteV2 set a null spriteFrame, reset the _contentSize to cocos2d::Size::ZERO. --- .../cocos/editor-support/creator/CCScale9Sprite.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/native/cocos/editor-support/creator/CCScale9Sprite.cpp b/native/cocos/editor-support/creator/CCScale9Sprite.cpp index b533230cc8..2f46d5fdc6 100755 --- a/native/cocos/editor-support/creator/CCScale9Sprite.cpp +++ b/native/cocos/editor-support/creator/CCScale9Sprite.cpp @@ -923,12 +923,18 @@ bool Scale9SpriteV2::setSpriteFrame(const std::string& sfName) } bool Scale9SpriteV2::setSpriteFrame(cocos2d::SpriteFrame* spriteFrame) -{ - if(!spriteFrame) return false; +{ CC_SAFE_RELEASE(this->_spriteFrame); this->_spriteFrame = spriteFrame; - CC_SAFE_RETAIN(spriteFrame); this->_quadsDirty = true; + + if(!spriteFrame) + { + this->setContentSize(cocos2d::Size::ZERO); + return true; + } + + CC_SAFE_RETAIN(spriteFrame); if(this->_contentSize.equals(cocos2d::Size::ZERO)) { this->setContentSize(spriteFrame->getRect().size); From 2fec8318f8284f7353d0db544bdd1d8db48c88ab Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 11 May 2018 10:46:02 +0800 Subject: [PATCH 2/2] Fix incorrect assertion if creating GLProgram in JS. (#1233) --- .../manual/jsb_cocos2dx_manual.cpp | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/native/cocos/scripting/js-bindings/manual/jsb_cocos2dx_manual.cpp b/native/cocos/scripting/js-bindings/manual/jsb_cocos2dx_manual.cpp index 2b8dc48073..8457abf74e 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_cocos2dx_manual.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_cocos2dx_manual.cpp @@ -870,6 +870,9 @@ static void onAfterDispatchTouchEvent(Event* event) static void onTextureCreate(TextureCache* cache, Texture2D* texture) { + if (cache == nullptr || texture == nullptr) + return; + se::AutoHandleScope hs; auto iterOwner = se::NativePtrToObjectMap::find(cache); if (iterOwner == se::NativePtrToObjectMap::end()) @@ -893,6 +896,9 @@ static void onTextureCreate(TextureCache* cache, Texture2D* texture) static void onTextureDestroy(TextureCache* cache, Texture2D* texture) { + if (cache == nullptr || texture == nullptr) + return; + se::AutoHandleScope hs; auto iterOwner = se::NativePtrToObjectMap::find(cache); if (iterOwner == se::NativePtrToObjectMap::end()) @@ -916,6 +922,9 @@ static void onTextureDestroy(TextureCache* cache, Texture2D* texture) static void onGLProgramCreate(GLProgramCache* cache, GLProgram* program) { + if (cache == nullptr || program == nullptr) + return; + se::AutoHandleScope hs; auto iterOwner = se::NativePtrToObjectMap::find(cache); if (iterOwner == se::NativePtrToObjectMap::end()) @@ -925,9 +934,23 @@ static void onGLProgramCreate(GLProgramCache* cache, GLProgram* program) } se::Value val; - bool fromCache = false; - native_ptr_to_seval(program, __jsb_cocos2d_GLProgram_class, &val, &fromCache); - if (!fromCache && val.isObject()) + // bool fromCache = false; + native_ptr_to_seval(program, __jsb_cocos2d_GLProgram_class, &val/* , &fromCache */); + // No need to check whether GLProgram se::Value is created from cache since we need to attach it GLProgramCache in both situations. + // Situation 1: fromCache is true which indicates GLProgram is created in JS, for example: + /* + var shader = new cc.GLProgram(); + if (shader.initWithString(vertexShader, framentShader_no_alpha)) { + ... + ... + cc.shaderCache.addProgram(shader, 'cp_shader_gray_mvp'); + } + */ + // Situation 2: fromCache is false which indicates GLProgram is created in C++ by engine. + // All built-in shaders are created in C++, onGLProgramCreate will be invoked in while initializing built-in shaders. + + // Therefore, don't make an assert here since it's correct for both situations. + if (/* !fromCache && */val.isObject()) { iterOwner->second->attachObject(val.toObject()); } @@ -939,6 +962,9 @@ static void onGLProgramCreate(GLProgramCache* cache, GLProgram* program) static void onGLProgramDestroy(GLProgramCache* cache, GLProgram* program) { + if (cache == nullptr || program == nullptr) + return; + se::AutoHandleScope hs; auto iterOwner = se::NativePtrToObjectMap::find(cache); if (iterOwner == se::NativePtrToObjectMap::end()) @@ -962,6 +988,9 @@ static void onGLProgramDestroy(GLProgramCache* cache, GLProgram* program) static void onGLProgramStateCreate(GLProgramStateCache* cache, GLProgramState* state) { + if (cache == nullptr || state == nullptr) + return; + se::AutoHandleScope hs; // NOTE: GLProgramStateCache is a private class in cocos2dx, it isn't included in cocos2d.h and also didn't expose to JS // Since it's a singleton class, we could attach 'state' argument to global object instead. @@ -981,6 +1010,9 @@ static void onGLProgramStateCreate(GLProgramStateCache* cache, GLProgramState* s static void onGLProgramStateDestroy(GLProgramStateCache* cache, GLProgramState* state) { + if (cache == nullptr || state == nullptr) + return; + se::AutoHandleScope hs; // NOTE: 'state' argument is attached to global object, therefore we need to detach it from global object. se::Object* global = se::ScriptEngine::getInstance()->getGlobalObject();