alecmce/xember — Issue #11: Removing an entity when iterating over a family stops the iteration
Author: richardlord
Created: 2011-10-16 10:41:23+00:00
State: open

If you remove the current item from the list your iterating over, the iteration will stop because node.next will be null on the node just removed. This will happen, for example, in the asteroids example collision system when checking collisions on the bullets...

for (bullet = userBullets.head as BulletCollisionNode; bullet; bullet = bullet.next)
{
...
entityCreator.destroyEntity(bullet.entity);
...
}

This can be solved in the game code by fetching the next node before processing the current one

for (bullet = userBullets.head as BulletCollisionNode; bullet; bullet = next)
{
next = bullet.next;
...
entityCreator.destroyEntity(bullet.entity);
...
}

but it would be better if the list could handle this itself. Although I don't yet have a clear idea how best to do this, the simplest option is not nulling the next and previous on the removed node, which feels messy but would do the job (importantly, the removed node is still free to be garbage collected when the currently executing code has finished with it).