Box2D body can not destroy while collied

Hello friends,
I have a issue regarding the collision of two bodies in box2d. For collision detection i am using ray wenderlich tutorial.
In my game i have two sprite body 1) Player 2) Enemy.
Here in game enemy body follow the player and when they collied enemy body will destroy but here in my case they collied perfectly and also detect collision point but my enemy body can not destroy.
I show you my update method code
Code:
-(void)update:(ccTime)dt
{
    world->Step(dt, 10, 1);
    //  CGPoint scaledVelocity = ccpMult(leftJoystick.velocity, 140);
    for(b2Body *b=world->GetBodyList();b;b=b->GetNext())
    {
        if(b->GetUserData()!=NULL)
        {
            CCSprite *balldata=(CCSprite *)b->GetUserData();
            balldata.position = ccp(b->GetPosition().x * PTM_RATIO,
                                    b->GetPosition().y * PTM_RATIO);
        }
    }
    playerPos=playerBody->GetPosition();
    enemyPos=enemyBody->GetPosition();
   // This code is for follow the enemy sprite...
    float dx=(playerPos.x-enemyPos.x)*PTM_RATIO;
    float dy=(playerPos.y-enemyPos.y)*PTM_RATIO;
    float d=sqrtf(dx*dx + dy*dy);
    float v=1;
    if(d>10)
    {
        enemyBody->SetTransform(b2Vec2(enemyPos.x+dx/d *v *dt,enemyPos.y+dy/d *v *dt), enemyBody->GetAngle());
    }
    std::vector<b2Body *>toDestroy;
    std::vector<MyContact>::iterator pos;
    for(pos = contactListener->_contacts.begin();
        pos != contactListener->_contacts.end(); ++pos) {
        MyContact contact = *pos;
        b2Body *bodyA = contact.fixtureA->GetBody();
        b2Body *bodyB = contact.fixtureB->GetBody();
        if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL)
        {
            CCSprite *spriteA = (CCSprite *) bodyA->GetUserData();
            CCSprite *spriteB = (CCSprite *) bodyB->GetUserData();
         // Playersprite Tag is 2      Enemy tag is 1

            if (spriteA.tag == 1 && spriteB.tag == 2)
            {
                // SpriteA = PlayerSprite
                // SpriteB = EnemySprite
           
            }
            else if (spriteA.tag == 2 && spriteB.tag == 1)
            {
                //toDestroy.push_back(bodyB);
                NSLog(@"Enemy Touch");
                NSString *t=[NSString stringWithFormat:@"Game Over"];
                label.string=t;
                [self removeChild:spriteB cleanup:YES];
                world->DestroyBody(bodyB);
            }
        }
    }
}


Please help me
Thanks
I am not really sure what is happening, but

a) No, a body cannot be destroyed while it is being collided, because the world is processing data involving the body, so if the body is destroyed it will cause trouble.

b) To solve the problem cause by (a) look at this tutorial by the irresistible force:

https://www.iforce2d.net/b2dtut/removing-bodies

It shows you how to remove bodies and some of the other tutorials will tell you how to detect collisions and other things like that.

Finally

c) This is in Games and Applications, and I am not sure why. A place like general discussion would probably be better.

Hope that helped.

Original thread: https://box2d.org/forum/viewtopic.php?t=8729