Author Topic: Access violation in the code below?  (Read 1572 times)

Micboy

  • Jr. Member
  • **
  • Posts: 79
  • Karma: +0/-2
    • View Profile
Access violation in the code below?
« on: December 19, 2012, 01:28:09 pm »
Could someone please have a look at this code:

error returned is:
Unhandled exception at 0x00a1ac42 in MyFirstApp.exe: 0xC0000005: Access violation reading location 0x00000010.


Code: [Select]
#include "DragonFireSDK.h"
#include <time.h>
#include <Box2D/Box2D.h>

// For iPhone:
const int SCREEN_W = 480;
const int SCREEN_H = 320;

// Scale is 32 pixels = 1 meter:
const float SCALE = 1.0f / 32.0f;

const float SCALED_SCREEN_W = 480*SCALE;
const float SCALED_SCREEN_H = 320*SCALE;
const float SCALED_HALF_SCREEN_W = 240*SCALE;
const float SCALED_HALF_SCREEN_H = 160*SCALE;

// Animation frequency is 30 times / second:
const float DELTA = 1.0f / 30.0f;

// Bodies are 50 pixels per side.
// Body size is halved, since body vertices are relative to the center of the shape:
const float BODY_SIZE_WIDTH = (40.f * SCALE) / 2.0f;
const float BODY_SIZE_HEIGHT = (60.f * SCALE) / 2.0f;

int imagehandleS;

//int jumpTouch;
//int OnJumpTouch(int id, int event, int x, int y);
//bool touching;

int spriteX;
int spriteY; //to control sprite location using variables manually
int jumpheightY;
int jumpY;

int groundX;
int groundY;
int frame = 0;

// Keeps track of the "world's" Y coordinate.
int GlobalY;

int viewS;
int viewG;
int viewB;

int spriteH_pixels = 60;
int spriteW_pixels = 40;
int spriteH_metres = 60*SCALE;
int spriteW_metres = 40*SCALE;

int jumpTouch;
int OnJumpTouch(int id, int event, int x, int y);
    b2Fixture* footSensorFixture;
    b2PolygonShape footSensorBox;
    b2FixtureDef FootSensorFixtureDef;

bool blTouching;

class MyContactListener : public b2ContactListener
{
    void BeginContact(b2Contact* contact)
    {
        //check if fixture A was a ball
        void* bodyUserData = contact->GetFixtureA()->GetUserData();
        if ( (int)bodyUserData ==3 )
        {
blTouching = true;
        }   
        //check if fixture B was a ball
        bodyUserData = contact->GetFixtureB()->GetUserData();
        if ( (int)bodyUserData ==3 )
        {
blTouching = true;
        } 
    }
 
    void EndContact(b2Contact* contact)
    {
        //check if fixture A was a ball
        void* bodyUserData = contact->GetFixtureA()->GetUserData();
        if ( (int)bodyUserData==3 )
        {
blTouching = false;
        } 
        //check if fixture B was a ball
        bodyUserData = contact->GetFixtureB()->GetUserData();
        if ( (int)bodyUserData==3 )
        { 
blTouching = false;
        }
    }
};

MyContactListener myContactListenerInstance;


// Earth gravity = 9.81 m/s:
const float EARTH_GRAVITY = 9.81f;
// Box2D objects:
b2World* world;
b2Vec2 Gravity;
b2Body* body;




void AppMain()
{
// Application initialization code goes here.  Create the items / objects / etc.
// that your app will need while it is running.
LandscapeMode();
spriteX=220;
spriteY=180;

groundX=0;
groundY=240; //remember that ground in DFSDK with ‘box’ ground in box2d

imagehandleS = ImageAdd("Images/sprite.png");//allocate handle to image

viewS = ViewAdd("Images/sprite.png", spriteX, spriteY); //add a view to image to make it visible

spriteH_pixels=ViewGetHeight(viewS); //sprite height in pixels
spriteW_pixels=ViewGetWidth(viewS); //sprite height in pixels
spriteH_metres=spriteH_pixels*SCALE; //sprite height in metres
spriteW_metres=spriteW_pixels*SCALE;//sprite width in metres

Gravity.Set(0.0f, EARTH_GRAVITY);               // Set default gravity vector:
const bool AllowSleep = true;
world = new b2World(Gravity, AllowSleep);
world->SetContactListener(&myContactListenerInstance);


b2BodyDef groundBodyDef; //initialise ground body
groundBodyDef.position.Set(0.0f, 240*SCALE);

//set ground position in container at (0,240) initially
b2Body* groundBody = world->CreateBody(&groundBodyDef); //create a ground polygon
b2PolygonShape groundBox;

groundBox.SetAsBox(240*SCALE, 60*SCALE);
//half width=240; half height=60

groundBody->CreateFixture(&groundBox, 0.0f); //creating a shape fixture for the ground body

//Creating dynamic bodies
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody; //by default == static, make them dynamic
bodyDef.position.Set(spriteX*SCALE, spriteY*SCALE);
b2Body* body = world->CreateBody(&bodyDef);

//creating and attaching a polygon shape using a fixture definition
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(1.0f, 1.50f);
//sprite pixel width = 40
//sprite pixel height = 60

b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f; //set density
fixtureDef.friction = 0.3f; //set friction
body->CreateFixture(&fixtureDef);


jumpTouch = TouchAdd(0,0, 320, 480, OnJumpTouch, 0);
footSensorBox.SetAsBox(10 * SCALE, 30 * SCALE);
    FootSensorFixtureDef.isSensor = true;
    FootSensorFixtureDef.shape =&footSensorBox;
    footSensorFixture = body->CreateFixture(&FootSensorFixtureDef);
    footSensorFixture->SetUserData( (void*)3 );

}

int OnJumpTouch(int id, int event, int x, int y)
{
    if (event ==1)
    {
if (blTouching == true)
{
body->ApplyLinearImpulse(b2Vec2(0, -10), body->GetWorldCenter());
}
}
    return id;
}
void AppExit()
{
// Application exit code goes here.  Perform any clean up your app might
// need to do in the event of interruption by a phone call or the user
// pressing the Home button, or some other event that would cause your
// application to terminate.
}


void OnTimer()
{
    // Main loop code goes here.  OnTimer() is called 30 times per second.

// Update all of the bodies created:
    world->Step(DELTA, 10, 10);
        // To convert Box2D positions to DFSDK positions, subrtract BODY_SIZE
        // from each coordinate to get the top left of the shape:
        ViewSetxy(viewS, ((body->GetPosition().x - BODY_SIZE_WIDTH) / SCALE), ((body->GetPosition().y - BODY_SIZE_HEIGHT) / SCALE));
        // Box2D uses radians for angles, but DFSDK uses degrees.  Convert. Degrees = Radians * 180 / PI:

}


DeadSn0wMan

  • Guest
Re: Access violation in the code below?
« Reply #1 on: December 19, 2012, 02:37:45 pm »
I haven't red through the code but I had that exact problem and it was caused by a variable that was used with out being initialised = NULL. Mine was a char* that I used for a TextAdd() but I never set any value to the char*

graham

  • Guest
Re: Access violation in the code below?
« Reply #2 on: December 19, 2012, 03:03:05 pm »
in "OnTimer" you are reading the variable "body" without having initialised it first.