只有在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()
是无法释放的,所以这就造成了内存泄露
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,第二次创建是,tt都是同一个,但是image发生了变化,直接复制就让之前的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) // 对应的纹理
{
}
ParticleSystem::~ParticleSystem()
{
_particleData.release();
CC_SAFE_RELEASE(_texture);// texture.ref=2,无法释放
}
Texture2D::~Texture2D()
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
VolatileTextureMgr::removeTexture(this);// 导致这个无法释放
#endif
}
void VolatileTextureMgr::removeTexture(Texture2D* t)
{
for (auto& item : _textures)
{
VolatileTexture* vt = item;
if (vt->_texture == t)// 对应的纹理
{
_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