我在cocos2d-x v.2.2.2中添加了Player* _player
指向我的场景的指针。我已经定义了,所以它被引用计数。我对HelloWorldScene
创建方法看起来像这样为什么不删除cocosd2d-x中的最后一个场景触发场景析构函数?
Scene* HelloWorldScene::createScene(Player* player)
{
auto scene = Scene::create();
auto layer = HelloWorldScene::create();
layer->_player = player;
player->retain();
scene->addChild(layer);
return scene;
}
其中player
在AppDelegate::applicationDidFinishLaunching()
实例化。现在,因为我已经保留_player
(我今天感觉像一个不错的家伙),我已经决定将其释放,以及:
HelloWorldScene::~HelloWorldScene()
{
if (_player)
{
_player->release();
}
}
到目前为止好。然而,当HelloWorldScene
被弹出,以下称为
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);
}
}
所以,每当HelloWorldScene
在栈中的最后一个场景,它不会被破坏? (至少从XCode看起来就是这样)
我不是C++特级大师,所以请原谅我的无知。然而,对我而言,这是非常意想不到的行为。不应该在程序终止之前清理弹出的场景(通过让_sendCleanupToScene = true
并让它运行一次迭代)?
显然,我错过了一些东西......如果有人能够对此有所了解,我会很高兴! :)