Crash at b2BlockAllocator.cpp, line 183

General Box2D issues or C++ specific issues
ljg66
Posts: 1
Joined: Wed Sep 17, 2014 11:33 am

Crash at b2BlockAllocator.cpp, line 183

Postby ljg66 » Fri Feb 20, 2015 12:08 am

Hello. I am using SFML and Box2D (version 2.3.0) to make a 2D platformer with shooting capabilities. In one of my levels, the player can spray water, which consists of sprite projectiles (I found other methods to create fluids, but it seemed like too much work for a simple game like this) that have a lifetime of 0.1 seconds. These projectiles are stored in a vector of active projectiles, but when their lifetime expires, they are moved to a vector for inactive projectiles to be deleted. When holding down the spacebar, the player can shoot a geyser of water for as long as he/she wants, but currently, I'm experiencing issues with this. At first, everything works properly, but after 20-30 projectiles are created and deleted whenever their lifetime expires, the game crashes, and I receive a message saying that the assertion at b2BlockAllocator.cpp, line 183 failed. The function that line resides in is b2BlockAllocator::Free(), and it is the following:

Code: Select all

b2Assert(   (int8*)p + blockSize <= (int8*)chunk->blocks ||
                  (int8*)chunk->blocks + b2_chunkSize <= (int8*)p);


I assume this is because the body was already deleted, but I don't understand why. Here are the precautions I am taking:

1. I do all the deletion AFTER the Step() function is called.
2. I included an if statement that prevents a body from getting deleted again after it was deleted the first time.
3. I assign NULL to the body right after it's been deleted.
4. I clear the vector after every body in it has been deleted because I read somewhere that the references still exist in the vector or something, even after all the pointers have been freed.

I've spent hours trying to figure out what the problem is, but I'm out of ideas. If anyone has encountered this problem before, please tell me how you managed to resolve it. Here's the code in question:

The Update() function for my level:

Code: Select all

void Level1::Update(Scene *currScene, bool gameOver, bool gamePaused, bool canPlayLevel, sf::RenderWindow *gameWin)
{
   if (canPlayLevel && !gameOver && !gamePaused)
        {
      world->Step(timeStep, velIts, posIts);

      --timeStepsLeft;   //Initially 6; using the tidbit from the Box2D tutorial on removing bodies safely, I decided to wait for 6 steps before deleting the bodies because the projectile lifetime is 0.1 seconds (0.1 * 60)

      if (timeStepsLeft == 0)
      {
         if (!GameMaster::GetCurrScene()->sceneInactivePProjectiles.empty() || !GameMaster::GetCurrScene()->sceneInactiveEProjectiles.empty())
            GameMaster::GetCurrScene()->DeleteInactiveProjs();

         timeStepsLeft = 6;
      }
        .
        .
        .
        }
}


And the code to delete projectiles:

Code: Select all

void Scene::DeleteInactiveProjs()
{
   size_t i;

   for (i = 0; i < sceneInactivePProjectiles.size(); i++)
   {
      if (sceneInactivePProjectiles[i].get()->bounds != NULL)
      {
         GameMaster::GetCurrLevel()->GetWorld()->DestroyBody(sceneInactivePProjectiles[i].get()->bounds);
         sceneInactivePProjectiles[i].get()->bounds = NULL;
      }
   }

   sceneInactivePProjectiles.clear();
}

Return to “Bugs, Requests, and Feedback”



Who is online

Users browsing this forum: No registered users and 1 guest