Hi guys, I’m having some memory management issues and I’m getting really confused by it, so I’d like to defer to you’re collective greater knowledge.
I have a game that switches between scenes, each scene is pretty standard (sprites, buttons etc). I suspected that Objects might not be getting deallocated properly and after profiling it appears I was right.
My code is much chunkier than what follows, but this should give you the idea of how I’m trying to go about doing things.
CCScene* MainMenuScreen::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
MainMenuScreen *layer = MainMenuScreen::create();
scene->addChild(layer);
return scene;
}
//********************************************************************
// Initilisation Method.
// Not called directly.
//********************************************************************
bool MainMenuScreen::init()
{
//////////////////////////////
if ( !CCLayer::init() )
{
return false;
}
/////////////////////////////
winSize = CCDirector::sharedDirector()->getWinSize();
_batchNode = CCSpriteBatchNode::create("FrontEndSprites.png");
this->addChild(_batchNode);
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("FrontEndSprites.plist");
// add "MainMenuScreen" splash screen"
m_spriteBackground = CCSprite::createWithSpriteFrameName("Background.png");
m_spriteBackground->setPosition( ccp(winSize.width/2, winSize.height/2) );
// add the sprite as a child to this layer
_batchNode->addChild(m_spriteBackground, 0);
//Button to move the screen forward
CCSprite* pPlayGameSprite = CCSprite::createWithSpriteFrameName("FE_Button_1_Pressed.png");
CCSprite* pPlayGameSpritePressed = CCSprite::createWithSpriteFrameName("FE_Button_1.png");
CCMenuItemSprite *pPlayGameButton = CCMenuItemSprite::create(
pPlayGameSprite,
pPlayGameSpritePressed,
this,
menu_selector(MainMenuScreen::MoveToPlayGame) );
pPlayGameButton->setPosition( ccp(65, 80) );
// create menu, it's an autorelease object
pPlayGameMenu = CCMenu::create(pPlayGameButton, NULL);
pPlayGameMenu->setTouchEnabled(false);
pPlayGameMenu->setPosition( CCPointZero );
pPlayGameMenu->setOpacity(0);
this->addChild(pPlayGameMenu, 1);
this->setTouchEnabled(false);
return true;
m_pSprite = CCSprite::createWithSpriteFrameName("FE_Electric_001.png");
m_pSprite->setPosition( ccp(winSize.width/2, (winSize.height/2) + 123.0f) );
_batchNode->addChild(m_pSprite, 2);
}
void MainMenuScreen::release(){
m_pSpriteAnimation->release();
m_pSpiteAnimateAction->release();
_batchNode->removeAllChildrenWithCleanup(true);
this->removeAllChildrenWithCleanup(true);
printf("Releasing Main Menu\n");
}
void MainMenuScreen::onExit(){
this->unscheduleAllSelectors();
this->release();
}
void MainMenuScreen::SetUpAnimation(){
CCArray *FrameArray = CCArray::create();
FrameArray->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("Frame_001.png"));
FrameArray->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("Frame_002.png"));
FrameArray->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("Frame_003.png"));
//Create an Animation Object
m_pSpriteAnimation = CCAnimation::create(FrameArray, (1.0f/FrameArray->count()));
m_pSpriteAnimation->setRestoreOriginalFrame(true);
m_pSpriteAnimation->setLoops(5);
m_pSpriteAnimation->retain();
//create the animate action and set up parameters
m_pSpriteAnimateAction = CCAnimate::create(m_pSpriteAnimation);
m_pSpriteAnimateAction->setDuration(1.0f);
m_pSpriteAnimateAction->retain();
}
I hope this is enough to give you guys an idea of what I’m trying to do in my code.
My understanding is, if you explicitly call retain() on any object you have to explicitly call release() when you have finished using the object. I am trying to do this in the release() method in my MainMenuScreen class.
My second scene is set up exactly the same way and when I move between them, the profiling tool clearly shows that the allocations go up after switching one, then remain at the same level after every other switch. Unless I’m mistaken, this means that nothing is being deallocated properly.
onExit(), Release() and the destructor are all being called.
Any help anyone can give me would be greatly appreciated.