diff --git a/native/cocos/editor-support/creator/CCScale9Sprite.cpp b/native/cocos/editor-support/creator/CCScale9Sprite.cpp index 1567c52aeb..0280b5b591 100755 --- a/native/cocos/editor-support/creator/CCScale9Sprite.cpp +++ b/native/cocos/editor-support/creator/CCScale9Sprite.cpp @@ -924,12 +924,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); 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 22f682fa93..73094177c9 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_cocos2dx_manual.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_cocos2dx_manual.cpp @@ -895,6 +895,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()) @@ -918,6 +921,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()) @@ -941,6 +947,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()) @@ -950,9 +959,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()); } @@ -964,6 +987,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()) @@ -987,6 +1013,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. @@ -1006,6 +1035,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();