Saturday, August 6, 2022
HomeGame DevelopmentDoable CCParticleSystem Leak - C++

Doable CCParticleSystem Leak – C++


I’ve observed that Cocos2d’s ParticleSystem makes use of image->launch() in initWithDictionary methodology. CCImage just isn’t an autoreleased object and I believe C++’s delete must 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. After I debug with printLeaks, it wasn’t deallocating the photographs that are retained by VolatileTexture after some time and the picture depend was over 1000. Does it deallocate the photographs mechanically in regular circumstances?

When the picture is added to VolatileTexture, it will increase its reference depend by 1 through a name to retain(), nevertheless it 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

In some unspecified time in the future the occasion of the VolatileTexture might 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 must be better than 0");
    --_referenceCount;

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

So, it does delete itself.

I’m unsure how VolatileTextureMgr works, or when it deletes textures and such, however simply going off the code, there isn’t any difficulty within the ParticleSystem. If there may be any form of downside, it’s most certainly within the VolatileTextureMgr or the way in which 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