If you try to do this inside the world function Step () (for example, in the contact listener), this will not work, because the world still processes bodies. You will need to indicate which bodies you want to destroy, and then destroy them after completing the world step.
Also, I'm not sure which language you are using, but it seems strange that this loop will destroy the body and then call GetNext () on the item you just destroyed.
source share