c++ - Removing a CCNode from std::list causes error in XCode -
i'm having trouble deleting items std::list
containing ccnode
object. xcode gives me following error when trying erase()
element:
error: address doesn't contain section points section in object file
.
or error:
exc_bad_access code=2
@ assembly file.
and crashes at:
ccglbindtexture2d( m_pobtexture->getname() );
giving me exc_bad_access.
each time run application 1 of errors.
the remove() method correctly removes ccnode cclayer, disappears , node count goes down one. problem testobject still remains in testlist list, eating memory, cpu, , messing game.
i wrote test case reproduce problem. here is:
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++); } }
the testobject class ccnode following remove()
, spawn()
methods added:
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); }
the stacktrace xcode gives me lists couple of internal update , render functions of cocos2dx, giving me no idea whats causing crash.
you doing testlist = *new list<testobject>;
wrong.
the right way just
testlist = list<testobject*>(); testlist.push_back(new testobject()); addchild(testlist.back());
as want store pointers.
in c++ *new something
instant memory leak. store copy of object.
Comments
Post a Comment