我無法刪除包含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,讓我不知道什麼導致崩潰。
謝謝!我最終通過使用向量來跟蹤CCNodes標記來解決問題。使用方法addChild(CCNode節點,int zIndex,int標記)和getChildByTagName(int標記)我可以從父CCLayer插入並獲取對象。儘管如此,你的建議仍然有幫助。 –