*************************************** Reprint Please specify the Source:Http://blog.csdn.net/lttree ******************************************
When using vectors in the cocos2d-x,
There was an error when deleting an object, it crashed ...
vector<bullet*>* bullets;//iterates through each Bullet, letting them update for themselves (Auto it = Bullets->begin (); It!=bullets->end (); it++) { (*it)->update (); Get bullet life, if the bullet has died out, release if ((*it)->getlife ()) { bubblet* b = *it; Bubblets->eraseobject (b); This->removechild (b,true); } }
There will be errors--vector iterators incompatible;
Maybe the way I opened it was wrong, so I used the C++11 method:
Vector<bullet*> bullets;for (auto& b:bullets) { b->update (); if (B->getlife ()) { bubblets.eraseobject (b); This->removechild (b,true); }}
or not ...
Looking for a long time, found that
It is said that because, when the iterator traversal, if the current to delete, then the back of the chaos, can not continue to go on,
So, it crashes.
So, if you traverse through an iterator, change it:
Iterate through each bullet and let them update for themselves (Auto it = Bullets->begin (); It!=bullets->end ();) { (*it)->update (); Get bullet life, if the bullet has died out, release if ((*it)->getlife ()) { bubblet* b = *it; it = Bubblets->eraseobject (b); This->removechild (b,true); } else { it++; }}
the movement of an iterator is no longer a loop, but a judgment statement.
Unfortunately, through the c++11 method of traversal, I have not thought how to change ...
*************************************** Reprint Please specify the Source:Http://blog.csdn.net/lttree ******************************************
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Cocos2d-x Vector--vector iterators Incompatible