I am trying to get used to manually call retain/release properly. But it seems I need help here.
From the figure with the blue highlight on one of the rows, that’s a memory leaked from using CCLabelTTF::labelWithString().
I would like to know the proper way release its when not use anymore.
FYI:
The testing game has 2 scene, one is for normal gameplay, and another is for showing a result. It will switch back and forth.
In addition, it’s weird that not every switching to another scene will generate the highlighted memory leaked (CCLabelTTF::labelWithString()). That label is static and normal to show some information in the game, so it will be used every time users play the game.
I use the code below for different case of those labels.
@
// create and initialize a label
CCLabelTTF* pLabel = CCLabelTTF::labelWithString(“Pew pew ~~”, “Thonburi”, 34);
// position the label on the center of the screen
pLabel~~>setPosition );
// add the label as a child to this layer
this~~>addChild(pLabel, 2);
// add special fire label
sprintf(m_specialNumUsageChars, “d/d”, m_numSpecialFire, NUM_SPECIAL_FIRE_USAGE);
m_specialFireNumUsageLabel = CCLabelTTF::labelWithString(m_specialNumUsageChars, “Thonburi”, 50);
m_specialFireNumUsageLabel~~>retain;
m_specialFireNumUsageLabel~~>setColor( ccc3(255, 0, 0));
m_specialFireNumUsageLabel~~>setPosition.y + 5));
this~~>addChild(m_specialFireNumUsageLabel, 2);
// add a counter of destroyed targets
sprintf(m_targetDestroyedCountChars, “%d”, _targetDestroyed);
m_targetDestroyedCountLabel = CCLabelTTF::labelWithString(m_targetDestroyedCountChars, “Thonburi”, 50);
m_targetDestroyedCountLabel~~>retain;
m_targetDestroyedCountLabel~~>setColor( ccc3(255, 0, 0));
m_targetDestroyedCountLabel~~>setPosition.width~~ 5,
m_specialFireButton->getPosition().y + 5));@
From the code above, we have 3 labels, the first one I didn’t call retain as I don’t interact with it during the gameplay session. But for another twos, I call retain() as I will change its string in real-time.
As well, when the game over I will switch to another scene and I did call release() explicitly before replacing the scene as well.
Anyway the code for releasing is still there in deallocate function just to make sure everything okay.
`HelloWorld::~HelloWorld()
{
…
if(m_specialFireNumUsageLabel)
{
m_specialFireNumUsageLabel->release();
m_specialFireNumUsageLabel = NULL;
}
if(m_targetDestroyedCountLabel)
{
m_targetDestroyedCountLabel->release();
m_targetDestroyedCountLabel = NULL;
}
}`
Sorry for a long post. But please help me to better understand how to handle the memory.
Thanks so much !
Screen Shot 2012-02-09 at 2.43.54 PM.png (100.9 KB)