Possible CCParticleSystem Leak

I have noticed that Cocos2d’s ParticleSystem uses image->release() in initWithDictionary method. CCImage is not an autoreleased object and I think C++'s delete should be used instead of releasing it. I have checked out cocos v4 and v3 and this hasn’t been changed. I added the code snippet from CCParticleSystem.cpp. I have checked the memory leaks with Ref::printLeaks method.

// 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);
  
setTexture(Director::getInstance()->getTextureCache()->addImage(image, _plistFile + textureName));

image->release();

Have you debugged it to verify?

In that specific area of code, the Image is created, used, and released, and since it should have a reference count of 1, it will be deleted.

I debugged this code again and I saw that it adds the image to VolatileTextureMgr and it increases the reference count by one. When I debug with printLeaks, it wasn’t deallocating the images which are retained by VolatileTexture after a while and the image count was over 1000. Does it deallocate the images automatically in normal conditions?

When the image is added to VolatileTexture, it increases its reference count by 1 via a call to retain(), but it also releases it in the destructor:

VolatileTexture::~VolatileTexture()
{
    CC_SAFE_RELEASE(_uiImage);
}

It’s still correct.

The sequence of events is as follows:

Image is created in particle system
Image refCount = 1
Image added to VolatileTextureMgr, wrapped in a VolatileTexture, so now refCount = 2
Image is released in particle system, so refCount = 1

At some point the instance of the VolatileTexture will be freed, and in its destructor it calls release() on the image. refCount = 1 at this point, so this is what happens:


void Ref::release()
{
    CCASSERT(_referenceCount > 0, "reference count should be greater than 0");
    --_referenceCount;

    if (_referenceCount == 0)
    {
        ....
        delete this;
    }
}

So, it does delete itself.

I’m not sure how VolatileTextureMgr works, or when it deletes textures and such, but just going off the code, there is no issue in the ParticleSystem. If there is any kind of problem, it’s most likely in the VolatileTextureMgr or the way it’s used.