Remove existing body (b2EdgeShape) when new body is created

Love this forum. I'm learning Box2D and Cocos2D and this forum has been very useful.

I'm creating B2Bodies with b2EdgeShape during ccTouchesMoved and I would like to have the existing body to be removed when I create a new body. I've spent over a week trying to destroy body or the fixture. I've tried setting existing body to inactive but no luck. I either destroy everything, destroy only new bodies or crash. Any hints or help would be appreciated. Below is the code for ccTouchesMoved to created the freehand draw bodies.

Code:
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
   
    UITouch *myTouch = [touches anyObject];
    CGPoint start = [myTouch locationInView: [myTouch view]];
    start = [[CCDirector sharedDirector] convertToGL: start];
    CGPoint end = [myTouch previousLocationInView:[myTouch view]];
    end = [[CCDirector sharedDirector] convertToGL:end];
   
    // brush
    NSValue *value= [NSValue valueWithCGPoint:start];
    [linePoints addObject:value ];
   
    float distance = ccpDistance(start, end);
   
    if (distance > 1)
    {
        int d = (int)distance;
       
       
        b2Vec2 s(start.x/PTM_RATIO, start.y/PTM_RATIO);
        b2Vec2 e(end.x/PTM_RATIO, end.y/PTM_RATIO);
       
        b2BodyDef bd;
        bd.type = b2_staticBody;
        bd.position.Set(0, 0);

        _brushBody = _world->CreateBody(&bd);
        _oldBrushBody = _brushBody;

   
        for (int i = 0; i < d; i++) {
           
            float difx = end.x - start.x;
            float dify = end.y - start.y;
            float delta = (float)i / distance;
            brush = [[CCSprite spriteWithFile:[NSString stringWithFormat:@"brush.png"]] retain];
            brush.scale = 2.0f;
            [brush setPosition:ccp(start.x + (difx * delta), start.y + (dify * delta))];
           
            [self addChild:brush];
            [lineSprites addObject:brush];
           
            // body
            b2EdgeShape brushShape;
            b2FixtureDef brushShapeDef;
            brushShapeDef.shape = &brushShape;
           
            brushShape.Set(b2Vec2(s.x, s.y), b2Vec2(e.x, e.y));
           
           _brushFixture = _brushBody->CreateFixture(&brushShapeDef);

        }
       
    }
   
}
You're saying this crashes? Where does it crash? I don't see anything being destroyed.
I also don't understand why you loop d times to create the exact same fixture inside the loop...
You can put more than one fixture on a body, so you don't really need to create a new body every time to add another fixture.
I agree the code above doesn't crash. I didn't post all my different attempts at destroying the existing body/fixture when a second is drawn. The above code keeps adding freehand line bodies to the scene; but what I'm trying to do is to remove the first freehand line body/fixture when a second is drawn.
Wouldn't you remove it in the touch down method?
I removed the fixture from the loop and that does make more sense. Then I try to destroy the body in the ccTouchesBegan but nothing seems to happen. Am I not using the right code to destroy the body? It seems like it should be straight forward. I'm still not sure what I'm missing.

Code:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   
    if (_brushBody != NULL) {
        _world -> DestroyBody(_brushBody);
        _brushBody = NULL;
}


Code:
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
   
    UITouch *myTouch = [touches anyObject];
    CGPoint start = [myTouch locationInView: [myTouch view]];
    start = [[CCDirector sharedDirector] convertToGL: start];
    CGPoint end = [myTouch previousLocationInView:[myTouch view]];
    end = [[CCDirector sharedDirector] convertToGL:end];
   
    NSValue *value= [NSValue valueWithCGPoint:start];
    [linePoints addObject:value ];
   
    float distance = ccpDistance(start, end);
   
    for (int i = 0; i < distance; i++) {
       
        float difx = end.x - start.x;
        float dify = end.y - start.y;
        float delta = (float)i / distance;
        brush = [[CCSprite spriteWithFile:[NSString stringWithFormat:@"brush.png"]] retain];
        brush.scale = 2.0f;
        [brush setPosition:ccp(start.x + (difx * delta), start.y + (dify * delta))];
       
        [self addChild:brush];
        [lineSprites addObject:brush];
       
    }
   
        b2Vec2 s(start.x/PTM_RATIO, start.y/PTM_RATIO);
        b2Vec2 e(end.x/PTM_RATIO, end.y/PTM_RATIO);
       
        // create brush body
        b2BodyDef bd;
        bd.type = b2_staticBody;
        bd.position.Set(0, 0);
        _brushBody = _world->CreateBody(&bd);
   
        // create brush shape
        b2EdgeShape brushShape;
        brushShape.Set(b2Vec2(s.x, s.y), b2Vec2(e.x, e.y));
   
        // Create shape definition and add to body
        b2FixtureDef brushShapeDef;
        brushShapeDef.shape = &brushShape;
        _brushFixture = _brushBody->CreateFixture(&brushShapeDef);
       
}
When you say nothing happens, you mean that the the debug draw display does not change?
Correct. Both the old line body and the new line body are present.
For the debug draw? Are you sure? If the body was destroyed and there were no errors, I don't see how the debug draw would have anything to draw after that.

You are removing the sprites from the layer, right?
The problem is the first created body is NOT being destroyed. I can't figure it out.
And how are you deciding that it did not get destroyed? Just by looking at the debug draw? Or by looking at your sprites? Or by dropping a dynamic object onto the body and seeing it bounce off?
What happens when you step into the DestroyBody function in the debugger? What happens when you step into the debug draw call after destroying the body?

How about taking the sprites out of the picture anyway, to make absolutely sure that you are looking at Box2D's debug draw display only.

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