I've added a Player* _player
pointer to my HelloWorldScene
scene in cocos2d-x v.2.2.2. I've defined class Player : public cocos2d::Object
, so it's referenced counted. My create method for HelloWorldScene
looks like this
Scene* HelloWorldScene::createScene(Player* player)
{
auto scene = Scene::create();
auto layer = HelloWorldScene::create();
layer->_player = player;
player->retain();
scene->addChild(layer);
return scene;
}
where player
is instantiated in AppDelegate::applicationDidFinishLaunching()
. Now, since I've retained _player
(and I feel like a nice guy today), I've decided to release it as well:
HelloWorldScene::~HelloWorldScene()
{
if (_player)
{
_player->release();
}
}
So far so good. However, when HelloWorldScene
is popped, the following is called
void Director::popScene(void)
{
CCASSERT(_runningScene != nullptr, "running scene should not null");
_scenesStack.popBack();
ssize_t c = _scenesStack.size();
if (c == 0)
{
end();
}
else
{
_sendCleanupToScene = true;
_nextScene = _scenesStack.at(c - 1);
}
}
So, whenever HelloWorldScene
is the last scene in the stack, it won't get destroyed? (At least that's what it seems like from XCode.)
I'm not a C++ grandmaster, so forgive my ignorance. Yet, to me, this is quite unexpected behavior. Shouldn't the popped scene be cleaned up before program termination (by letting _sendCleanupToScene = true
and having it run one more iteration)?
Clearly, I'm missing something ... If anyone could shed some light on this, I'd be thrilled! :)
So the premise of my question was wrong - I somehow got the idea that everything had to be released at program termination. But, as I re-learned yesterday, the OS will of course reclaim the allocated memory once the program terminates. Thus making releasing the last scene in the stack unnecessary.