2014-03-06 16 views
0

我在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; 
} 

其中playerAppDelegate::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并让它运行一次迭代)?

显然,我错过了一些东西......如果有人能够对此有所了解,我会很高兴! :)

回答

0

所以我的问题的前提是错误的 - 我不知何故有一个想法,一切都必须在程序终止发布。但是,正如我昨天重新学习的那样,一旦程序终止,操作系统当然会回收分配的内存。因此释放栈中的最后一个场景是不必要的。