只有在Android平臺,才啟用了CC_ENABLE_CACHE_TEXTURE_DATA
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
#define CC_ENABLE_CACHE_TEXTURE_DATA 1
#else
#define CC_ENABLE_CACHE_TEXTURE_DATA 0
#endif
在CCParticleSystem中
if( dictionary.find("textureImageData") != dictionary.end() ){
// ...
// For android, we should retain it in VolatileTexture::addImage which invoked in Director::getInstance()->getTextureCache()->addUIImage()
image = new (std::nothrow) Image();
bool isOK = image->initWithImageData(deflated, deflatedLen);
CCASSERT(isOK, "CCParticleSystem: error init image with Data");
CC_BREAK_IF(!isOK);
auto texture = Director::getInstance()->getTextureCache()->addImage(image, _plistFile + textureName);// texture.ref=1
setTexture(texture);// texture.ref=2
image->release();// 無法釋放
}
注釋中也說明了,在Android平臺,Image會額外的retain一次,所以這個的image->release()是無法釋放的,所以這就造成了內(nèi)存泄露
image->release()無法釋放的原因:
CCTextureCache.cpp中
Texture2D* TextureCache::addImage(Image* image, const std::string& key){
// ...
#if CC_ENABLE_CACHE_TEXTURE_DATA
VolatileTextureMgr::addImage(texture, image);
#endif
}
std::list<VolatileTexture*> VolatileTextureMgr::_textures;
void VolatileTextureMgr::addImage(Texture2D* tt, Image* image)
{
if (tt == nullptr || image == nullptr)
return;
VolatileTexture* vt = findVolotileTexture(tt);
image->retain(); // retain
vt->_uiImage = image;// 同一個plist,第二次創(chuàng)建是,tt都是同一個,但是image發(fā)生了變化,直接復(fù)制就讓之前的image變成了野指針
vt->_cashedImageType = VolatileTexture::kImage;
}
VolatileTexture* VolatileTextureMgr::findVolotileTexture(Texture2D* tt)
{
VolatileTexture* vt = nullptr;
for (const auto& texture : _textures)
{
VolatileTexture* v = texture;
if (v->_texture == tt)
{
vt = v;
break;
}
}
if (!vt)
{
vt = new (std::nothrow) VolatileTexture(tt);
_textures.push_back(vt);
}
return vt;
}
VolatileTexture::VolatileTexture(Texture2D* t)
: _texture(t) // 對應(yīng)的紋理
{
}
ParticleSystem::~ParticleSystem()
{
_particleData.release();
CC_SAFE_RELEASE(_texture);// texture.ref=2,無法釋放
}
Texture2D::~Texture2D()
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
VolatileTextureMgr::removeTexture(this);// 導(dǎo)致這個無法釋放
#endif
}
void VolatileTextureMgr::removeTexture(Texture2D* t)
{
for (auto& item : _textures)
{
VolatileTexture* vt = item;
if (vt->_texture == t)// 對應(yīng)的紋理
{
_textures.remove(vt);
delete vt;
break;
}
}
}
VolatileTexture::~VolatileTexture()
{
CC_SAFE_RELEASE(_uiImage);
}
Sprite為啥沒有這個問題呢?
Texture2D* addImage(Image *image, const std::string &key);// 有vt
Texture2D* addImage(const std::string &filepath, bool bSpriteFrame);// 沒有vt