What causes this assertion failure

I am trying to destroy bodies, and all was working well until I created 2 sensors. Now when the world tries to remove the sensor bodies I get:

Code:
Assertion failed: (IsLocked() == false), function DestroyBody


cause in b2World->DestoryBody?

Any ideas appreciated. Is this linked to how the world destroys sensors, does it matter if the sensor is actively colliding when it is destroyed?

Regards

i
You cannot destroy a body inside a callback. This is discussed in the manual and there are examples of how to do this correctly in the testbed.
Hi,

I am not destroying them in a call back as far as I am aware , they are being added to an array for disposal, and then there is a scheduled event called which destroys bodies which have been added to this nuke array. Its actually a commercial product called LevelSVG from Cocos2D author.

It works fine, until I add these 2 sensors to be destroyed.

Code:
-(void) touchedByBullet:(id)bullet
{
   currentHits++;
   hitLimit--;
   
   if (currentHits==smokeLimit)
   {
      //[smoke_node_ startEmitter];
   }
   else if (currentHits >= hitLimit)
   {
      sound_explosion_1.looping = NO;
      sound_explosion_1.gain = 1.0f;
      [sound_explosion_1 play];
      [game_ increaseScore:10];
      
      //[smoke_node_ stopEmitter];
      //[explosion_node_ startEmitter];
      
      // TAKE THESE COMMENTS OUT AND I GET THE ASSERTION FAILURE
      //[game_ removeB2Body:_radiusSensorBody];
      //[game_ removeB2Body:_frontSensorBody];
      
      
      [game_ removeB2Body:body_];
      [game_ removeB2Body:frontLeftWheel];
      [game_ removeB2Body:frontRightWheel];
      [game_ removeB2Body:rearLeftWheel];
      [game_ removeB2Body:rearRightWheel];
         hitLimit = 100000;
      
   }   
}







Code:

-(void) removeB2Body:(b2Body*)body
{
   NSAssert( nukeCount < kMaxNodesToBeRemoved, @"LevelSVG: Increase the kMaxNodesToBeRemoved in GameConstants.h");

   nuke[nukeCount++] = body;
   
}


// The Hero class processes its collisions, and anything which needs to to be deleted
// is cached for deletion into the nuke[] array for deleting during this scheduled selector.
-(void) removeB2Bodies

{
   // Sort the nuke array to group duplicates.
   std::sort(nuke, nuke + nukeCount);
   
   // Destroy the bodies, skipping duplicates.
   unsigned int i = 0;
   while (i < nukeCount)
   {
      b2Body* b = nuke[i++];
      while (i < nukeCount && nuke[i] == b)
      {
         ++i;
      }
      
      world_->DestroyBody(b);
      
      BodyNode *node = (BodyNode*) b->GetUserData();
      [node removeFromParentAndCleanup:YES];
   }
   
   nukeCount = 0;
}




Is the problem that sensors are probably still in contact with something at the point I am trying to delete them? If thats the case, the sensors will always be in contact with something...they are large sensors attached to the front of top own cars for obstacle avoidance....but I need to remove the entire car and sensors when hit by a bullet.

The technique being used is to add bodies to an array for future processing (as per the samples I have read).

Regards

i
Are you able to see the call stack when the assertion is hit? Please show us the entire call stack.
Hi Erin,

First I have to work out how to dump a callstack.!

I actually noticed that although the error only occurs when I remove the sensors, the actual error is raised by the bullet itself removing itself from the scene with a direct call to world->destroyBody(). The earlier code I posted was the car removing its constituent parts once it has been hit by a bullet.

Although this method below isnt called from within the collision callback itself - this is a separate scheduled tick each bullet makes on itself, which checks for the state of the bullet and removes it as appropriate.

Not sure why the error manifests itself only when I try to remove the 2 sensors from the car which has been hit by the bullet, but if I comment this out - everything runs fine.

Code:

// tick in the bullet which has hit the car
-(void) update:(ccTime)dt
{
   // anti-gravity force not needed!
   //b2Vec2 gravity = world_->GetGravity();
   //b2Vec2 p = body_->GetLocalCenter();
   //body_->ApplyForce( -body_->GetMass()*gravity, p );
   
   elapsedTime_ += dt;
   if( elapsedTime_ > bulletLifeSeconds_ ) {
      
      removeMe_ = YES;
   }
   
   if( removeMe_ ) {
      world_->DestroyBody(body_);
      //[[self parent] removeChild:self cleanup:YES];   
      [self setState:kBulletReady];
   }
   
}



Regards

i
inside bullet, more complete code

Code:

// scheduled tick inside bullet
-(void) update:(ccTime)dt
{
   // anti-gravity force
   b2Vec2 gravity = world_->GetGravity();
   b2Vec2 p = body_->GetLocalCenter();
   body_->ApplyForce( -body_->GetMass()*gravity, p );
   
   elapsedTime_ += dt;
   if( elapsedTime_ > BULLET_LIFE_SECONDS ) {

      removeMe_ = YES;
   }
   
   if( removeMe_ ) {
      
      world_->DestroyBody(body_);
      [[self parent] removeChild:self cleanup:YES];   
      [self setState:kBulletReady];
   }
   
}


// on contact with something, set bullet to be removed - it will be removed at the next scheduled update() tick
-(void) beginContact:(b2Contact*)contact
{
   b2Fixture* fixtureA = contact->GetFixtureA();
   b2Fixture* fixtureB = contact->GetFixtureB();
   NSAssert( fixtureA != fixtureB, @"Bullet: Box2d bug");
   
   b2Body *bodyA = fixtureA->GetBody();
   b2Body *bodyB = fixtureB->GetBody();
   
   NSAssert( bodyA != bodyB, @"Bullet: Box2d bug");
   
   // Box2d doesn't guarantees the order of the fixtures
   b2Body *otherBody = (bodyA == body_) ? bodyB : bodyA;
   b2Fixture *otherFixture = (bodyA == body_) ? fixtureB : fixtureA;
   
   // ignore sensonrs
   if( ! otherFixture->IsSensor() ) {

      // if a bullets collide with something, remove the bullet
      removeMe_ = YES;
            
      BodyNode *bNode = (BodyNode*) otherBody->GetUserData();
      if( bNode && [bNode conformsToProtocol:@protocol(BodyNodeBulletProtocol) ] )
         [(id<BodyNodeBulletProtocol>)bNode touchedByBullet:self];
   }
}


This is covered in the FAQ. http://code.google.com/p/box2d/wiki/FAQ

Quote:
Programming
You should have a working knowledge of C++ before you use Box2D. You should understand classes, inheritance, and pointers. There are plenty of resources on the web for learning C++. You should also understand your development environment: compilation, linking, and debugging.


You should learn C++ and your development environment before trying to tackle a Box2D project.
It seems linked to a limit imposed on the number of bodies to be deleted on each cycle.

This limit is set to 6 currently (kMaxNodesToBeRemoved) in my implementation. Increasing this gives me more stability.

Code:

-(void) removeB2Body:(b2Body*)body
{
   NSAssert( nukeCount < kMaxNodesToBeRemoved, @"LevelSVG: Increase the kMaxNodesToBeRemoved in GameConstants.h");

   nuke[nukeCount++] = body;
   
}




stack:

Code:

#0  0x98d55ef6 in __kill ()
#1  0x98d55ee8 in kill$UNIX2003 ()
#2  0x98de862d in raise ()
#3  0x98dfe6e4 in abort ()
#4  0x98deb28c in __assert_rtn ()
#5  0x0003cfa2 in b2World::DestroyBody (this=0x7889200, b=0x69a4c80) at /Users/stephen/Documents/XCode/Cocos2d/LevelSVG-2010-08-11/libs/Box2D/Dynamics/b2World.cpp:109
#6  0x000216f7 in -[MachineGunBullet_1 update:] (self=0x66f2cd0, _cmd=0xe8f34, dt=28.2493877) at /Users/stephen/Documents/XCode/Cocos2d/LevelSVG-2010-08-11/Classes/MachineGunBullet_1.mm:373
#7  0x000a282d in -[CCScheduler tick:] (self=0x714dc30, _cmd=0xeb4cc, dt=28.2493877) at /Users/stephen/Documents/XCode/Cocos2d/LevelSVG-2010-08-11/libs/cocos2d/CCScheduler.m:550
#8  0x00066cb8 in -[CCDirector drawScene] (self=0x711a490, _cmd=0xeb108) at /Users/stephen/Documents/XCode/Cocos2d/LevelSVG-2010-08-11/libs/cocos2d/CCDirector.m:235
#9  0x0006a82c in -[CCDisplayLinkDirector preMainLoop:] (self=0x711a490, _cmd=0xeac3d, sender=0x714e560) at /Users/stephen/Documents/XCode/Cocos2d/LevelSVG-2010-08-11/libs/cocos2d/CCDirector.m:1212
#10 0x007f2ebe in CA::Display::DisplayLink::dispatch ()
#11 0x007f3003 in CA::Display::EmulatorDisplayLink::callback ()
#12 0x029e2d43 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ ()
#13 0x029e4384 in __CFRunLoopDoTimer ()
#14 0x02940d09 in __CFRunLoopRun ()
#15 0x02940280 in CFRunLoopRunSpecific ()
#16 0x029401a1 in CFRunLoopRunInMode ()
#17 0x03ea02c8 in GSEventRunModal ()
#18 0x03ea038d in GSEventRun ()
#19 0x00898b58 in UIApplicationMain ()
#20 0x0000367a in main (argc=1, argv=0xbfffeedc) at /Users/stephen/Documents/XCode/Cocos2d/LevelSVG-2010-08-11/main.m:13




so my best guess is, the assertion is failing

Code:
NSAssert( nukeCount < kMaxNodesToBeRemoved, @"LevelSVG: Increase the kMaxNodesToBeRemoved in GameConstants.h");



and so the next line

Code:
nuke[nukeCount++] = body;


is writing outside the bounds of the array, and causing SIGABRT.

So - as I don't know how many bodies will need to be in this array at run time, is the solution just to start with a value such as 20, but copy the array into a new larger array if the limit gets blown?
Regards

i

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