From 08126023b4922cf620ccd014336a8780e626a8b0 Mon Sep 17 00:00:00 2001 From: James Chen Date: Sat, 16 Feb 2019 22:13:10 +0800 Subject: [PATCH] Fix memory leak if invoking Texture2D::setAlphaTexture many times. --- cocos/renderer/CCTexture2D.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cocos/renderer/CCTexture2D.cpp b/cocos/renderer/CCTexture2D.cpp index ec7adef83449..218c85ebd8ff 100644 --- a/cocos/renderer/CCTexture2D.cpp +++ b/cocos/renderer/CCTexture2D.cpp @@ -1499,10 +1499,12 @@ void Texture2D::removeSpriteFrameCapInset(SpriteFrame* spriteFrame) /// halx99 spec, ANDROID ETC1 ALPHA supports. void Texture2D::setAlphaTexture(Texture2D* alphaTexture) { - if (alphaTexture != nullptr) { - this->_alphaTexture = alphaTexture; - this->_alphaTexture->retain(); - this->_hasPremultipliedAlpha = true; // PremultipliedAlpha should be true. + if (alphaTexture != nullptr) + { + alphaTexture->retain(); + CC_SAFE_RELEASE(_alphaTexture); + _alphaTexture = alphaTexture; + _hasPremultipliedAlpha = true; // PremultipliedAlpha should be true. } }