[MVS C++ 2010/Windows 7] b2BlockAllocator crash at runtime?

Windows 7
SFML 1.6
Box2D 2.1.2
Visual Studio C++ 2010 Express


I'm at a complete loss, I've been trying to get SFML and Box2D to work together for quite some time now and it compiles successfully but crashes upon runtime. When debugged inside VS it breaks at line 108 in the b2BlockAllocator.cpp, giving the error:

Quote:
Unhandled exception at 0x00228513 in Hello World.exe: 0xC0000005: Access violation reading location 0x00000020.


Here's my code(I hope it's not too messy 🙂):

Code:
//SFML specific headers
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>

//Box2D specific headers
#include <box2d/Box2D.h>


//Structure to hold all need variables to create the world
struct box2DWorld
{
   //b2AABB worldAABB;
   float32 scale;
   b2Vec2 gravity;
   bool doSleep;
   b2World* world;
};

//Structure to hold all needed variables to create an object
struct box2DObject
{
   sf::Image image;
   sf::Sprite sprite;

   b2BodyDef bodyDef;
   b2Body* body;
   b2PolygonShape polyShape;
   b2FixtureDef fixtureDef;
   b2BodyDef circleDef;
   b2Vec2 position;
   float angle;

};

//Const Global for Pixel to Meter Ratio
const float RATIO = 9;

//Const Global for Time Step
const float32 timeStep = 1.0f / 60.0f;

//Const Global for Iterations
int32 velocityIterations = 10;
int32 positionIterations = 10;

//Const Global for PI
const double PI = 3.14;

//SFML Global Shapes
sf::Shape Floor = sf::Shape::Rectangle(0, 0, 800, 20, sf::Color(  0, 200,   0, 155));
sf::Shape Box   = sf::Shape::Rectangle(0, 0,  50, 50, sf::Color(127,   0,   0, 155));

//SFML Window 800x600 Pixels and 32 bits per pixel
sf::RenderWindow Game(sf::VideoMode(800, 600, 32), "SFML and Box2D Physics");

//World, Floor, and Box objects
box2DWorld myWorld;
box2DObject myFloor;
box2DObject myBox;
box2DObject myBox2;

//Forward Declarations
void createWorld();
void createFloor();
void createBox();
void updateWorld();
void updateObjects();
void drawObjects();
void handleInput();
void cleanup();
      

int main(int argc, char** argv)
{   
   //Create the event handler
   sf::Event Event;

   //Create the world
   createWorld();

   //Create the Floor
   createFloor();

   //Create the Box
   createBox();

   float angle = 0.0f;

   //The Game loop
   while(Game.IsOpened())
   {
      while(Game.GetEvent(Event))
      {
         //If Game window is X'd out
         if(Event.Type == sf::Event::Closed)
         {
            //Close the Game
            Game.Close();
         }
      }

      //Handle user input
      handleInput();

      //Clear the game window
      Game.Clear(sf::Color(100, 149, 237, 255));

      //Update the world
      updateWorld();

      //Update the objects
      updateObjects();

      //Draw objects
      drawObjects();

      //Draw to game Window
      Game.Display();
   }

   // Clean up
   cleanup();

   return EXIT_SUCCESS;
}

void createWorld()
{
   printf("Creating the world...\n");

   //set gravity
   myWorld.gravity = b2Vec2(0.0f, -9.8f);

   //allow objects to sleep and not do calculations
   myWorld.doSleep = true;

   //initialize world
   b2World world(myWorld.gravity, myWorld.doSleep);
}

void createFloor()
{
   //Set the floor's SFML center
   Floor.SetCenter(400, 10);

   //set the position of the body definition
   myFloor.bodyDef.position.Set(400.0f / RATIO, 590.0f / RATIO);
   
   //Define the extents of the polygon shape
   myFloor.polyShape.SetAsBox(400.0f / RATIO, 10.0f / RATIO);

   //create floor body in the world
   myFloor.body = myWorld.world->CreateBody(&myFloor.bodyDef);

   myFloor.body->CreateFixture(&myFloor.polyShape, 0.0f);

}

void createBox()
{
   printf("Creating the box...\n");

   //Set the box's SFML center
   Box.SetCenter(25, 25);

   
   myBox.bodyDef.type = b2_dynamicBody;
   myBox.bodyDef.position.Set(600.0f / RATIO, 50.0f / RATIO);

   myBox.body = myWorld.world->CreateBody(&myBox.bodyDef);

   
   myBox.polyShape.SetAsBox(Box.GetCenter().x / RATIO, Box.GetCenter().y / RATIO);

   //Create the box shape (polygon) definition
   myBox.fixtureDef.shape         = &myBox.polyShape;
   myBox.fixtureDef.friction      = 0.2f;
   myBox.fixtureDef.restitution   = 0.3f;
   myBox.fixtureDef.density      = 0.7f;
}

void updateWorld()
{
   myWorld.world->Step(timeStep, velocityIterations, positionIterations);
}

void updateObjects()
{
   //Get angle from bodies
   myBox.angle = myBox.body->GetAngle();

   //Get the position of the bodies
   myBox.position = myBox.body->GetPosition();

   //Connect shape with Box2D's myBox object
   Box.SetPosition(myBox.body->GetPosition().x * RATIO, myBox.body->GetPosition().y * RATIO);
   Box.SetRotation(-(myBox.angle * 180) / PI);

   //Connect shape with Box2D's myFloor object
   Floor.SetPosition(myFloor.body->GetPosition().x * RATIO, myFloor.body->GetPosition().y * RATIO);
}

void drawObjects()
{
   //Draw the floor shape
   Game.Draw(Floor);

   //Draw the box shape
   Game.Draw(Box);
}

void handleInput()
{
   //Close the program if ESC is pressed
   if(Game.GetInput().IsKeyDown(sf::Key::Escape))
   {
      //Close Game
      Game.Close();
   }

   //Give myBox Impulse to lift off floor
   if(Game.GetInput().IsKeyDown(sf::Key::Space))
   {
      myBox.body->ApplyLinearImpulse(b2Vec2(0,-5), b2Vec2(myBox.position.x, myBox.position.y));
   }
   //Give myBox Force to move left and right,
   //because of the friction, myBox won't move on objects
   //such as the floor or myBox2, it will move once in the
   //air however
   if(Game.GetInput().IsKeyDown(sf::Key::A))
   {
      myBox.body->ApplyForce(b2Vec2(-5, 0), b2Vec2(myBox.position.x, myBox.position.y));
   }
   if(Game.GetInput().IsKeyDown(sf::Key::D))
   {
      myBox.body->ApplyForce(b2Vec2(5, 0), b2Vec2(myBox.position.x, myBox.position.y));
   }
}

void cleanup()
{
   //Here we output to console several times
   //the reason being if your program bombs
   //at the end you will know that it bombed
   //cleaning up the bodies if the next output
   //doesn't show up or same for the rest

   printf("Cleaning up the bodies...");
   //Destroy the bodies
   myWorld.world->DestroyBody(myBox.body);
   myWorld.world->DestroyBody(myFloor.body);

   printf("Setting bodies to NULL...\n");
   //Set the bodies to NULL
   myBox.body = NULL;
   myFloor.body = NULL;

   printf("Deleting the world...\n");
   //Delete the world
   delete myWorld.world;

   printf("Cleanup completed!\n");
}


I've tried looking in the forums but have been unable to find something to help me. If someone can help me out or point me in the right direction it'd be greatly appreciated.

Regards,
Joe
JCSopko wrote:
void createWorld()
{
   printf("Creating the world...\n");

   //set gravity
   myWorld.gravity = b2Vec2(0.0f, -9.8f);

   //allow objects to sleep and not do calculations
   myWorld.doSleep = true;

   //initialize world
   b2World world(myWorld.gravity, myWorld.doSleep);
}


You created the world on the stack and it got freed. myWorld.world did not get initialized.
Oh man I feel stupid. Hahah, you even explicitly say to watch for that in the documentation. Thanks Erin, your help and all your work on Box2D doesn't go unappreciated.

Joe

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