Monday, August 8, 2022
HomeGame DevelopmentAttainable CCParticleSystem Leak - C++

Attainable CCParticleSystem Leak – C++


I’ve seen that Cocos2d’s ParticleSystem makes use of image->launch() in initWithDictionary methodology. CCImage just isn’t an autoreleased object and I feel C++’s delete ought to be used as a substitute of releasing it. I’ve checked out cocos v4 and v3 and this hasn’t been modified. I added the code snippet from CCParticleSystem.cpp. I’ve checked the reminiscence leaks with Ref::printLeaks methodology.

// For android, we should always retain it in VolatileTexture::addImage which invoked in Director::getInstance()->getTextureCache()->addUIImage()
picture = new (std::nothrow) Picture();
bool isOK = image->initWithImageData(deflated, deflatedLen);
CCASSERT(isOK, "CCParticleSystem: error init picture with Knowledge");
CC_BREAK_IF(!isOK);
  
setTexture(Director::getInstance()->getTextureCache()->addImage(picture, _plistFile + textureName));

image->launch();


Have you ever debugged it to confirm?

In that particular space of code, the Picture is created, used, and launched, and because it ought to have a reference depend of 1, will probably be deleted.

I debugged this code once more and I noticed that it provides the picture to VolatileTextureMgr and it will increase the reference depend by one. Once I debug with printLeaks, it wasn’t deallocating the pictures that are retained by VolatileTexture after some time and the picture depend was over 1000. Does it deallocate the pictures routinely in regular circumstances?

When the picture is added to VolatileTexture, it will increase its reference depend by 1 by way of a name to retain(), but it surely additionally releases it within the destructor:

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

It’s nonetheless appropriate.

The sequence of occasions is as follows:

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

Sooner or later the occasion of the VolatileTexture will probably be freed, and in its destructor it calls launch() on the picture. refCount = 1 at this level, so that is what occurs:


void Ref::launch()
{
    CCASSERT(_referenceCount > 0, "reference depend ought to be better than 0");
    --_referenceCount;

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

So, it does delete itself.

I’m undecided how VolatileTextureMgr works, or when it deletes textures and such, however simply going off the code, there is no such thing as a problem within the ParticleSystem. If there may be any sort of downside, it’s most probably within the VolatileTextureMgr or the best way it’s used.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments