2013-07-25 45 views
0

我無法刪除包含CCNode對象的std::list中的項目。當嘗試erase()一個元素時,XCode給了我以下錯誤:從std :: list中刪除CCNode會導致XCode中的錯誤

error: address doesn't contain a section that points to a section in a object file

或者這個錯誤:

EXC_BAD_ACCESS code=2在彙編文件。

,有時在崩潰:

ccGLBindTexture2D(m_pobTexture->getName());給我一個EXC_BAD_ACCESS。

每次運行應用程序時,我都會遇到其中一個錯誤。

remove()方法正確地從CCLayer中刪除了CCNode,它消失並且節點數量減少了一個。問題在於TestObject仍然保留在testList列表中,消耗了內存,cpu,並搞亂了遊戲。

我寫了一個測試用例來重現問題。那就是:

testList = *new list<TestObject>; 
testList.push_back(*new TestObject()); 
addChild(&testList.back()); 
testList.back().spawn(); 
testList.back().remove(); 

std::list<TestObject>::iterator test = testList.begin(); 
while (test != testList.end()) 
{ 
    if(test->isRemoved){ 
     testList.erase(test++); 
    } 
} 

的TestObject的類只是具有以下remove()spawn()方法CCNode補充說:

TestObject::TestObject(){ 
    sprite = *CCSprite::createWithTexture(MainScene::hostileship_tex); 
} 

void TestObject::spawn(){ 
    CCSize size = sprite.getTexture()->getContentSize(); 
    this->setContentSize(size); 
    this->addChild(&sprite); 
} 

void TestObject::remove(){ 
    GameLayer::getInstance().removeChild(this, true); 
} 

堆棧跟蹤的XCode給我只是列舉了幾個內部更新和渲染的功能cocos2dx,讓我不知道什麼導致崩潰。

回答

0

您正在做testList = *new list<TestObject>;錯誤。

正確的方法做它只是

testList = list<TestObject*>(); 
testList.push_back(new TestObject()); 
addChild(testList.back()); 

只要你想存儲指針。

在C++ *new Something是一個即時內存泄漏。此外,您將存儲該對象的副本。

+0

謝謝!我最終通過使用向量來跟蹤CCNodes標記來解決問題。使用方法addChild(CCNode節點,int zIndex,int標記)和getChildByTagName(int標記)我可以從父CCLayer插入並獲取對象。儘管如此,你的建議仍然有幫助。 –

相關問題