Add NULL initializers for pointers.

Constructing invalid shapes, e.g. a rectangle with width 0, could result
in a b2Assert to fail. In turn, Shape::~Shape tried to destroy a the shape
with an uninitialized pointer, yielding a segfault. Add NULL-initializers
and checks for NULL to prevent this.
This commit is contained in:
vrld
2011-01-25 13:24:39 +01:00
parent 98406370eb
commit 1c7e34715b
11 changed files with 16 additions and 13 deletions
+2 -1
View File
@@ -37,8 +37,9 @@ namespace box2d
def->radius = body->world->scaleDown(def->radius);
radius = def->radius;
this->localPosition = def->localPosition;
def->userData = (void*)data;
shape = body->body->CreateShape(def);
shape->SetUserData((void*)data);
}
CircleShape::~CircleShape()
+1 -1
View File
@@ -31,7 +31,7 @@ namespace physics
namespace box2d
{
DistanceJoint::DistanceJoint(Body * body1, Body * body2, float x1, float y1, float x2, float y2)
: Joint(body1, body2)
: Joint(body1, body2), joint(NULL)
{
b2DistanceJointDef def;
def.Initialize(body1->body, body2->body, world->scaleDown(b2Vec2(x1,y1)), world->scaleDown(b2Vec2(x2,y2)));
+1 -1
View File
@@ -31,7 +31,7 @@ namespace physics
namespace box2d
{
GearJoint::GearJoint(Joint * joint1, Joint * joint2, float ratio)
: Joint(joint1->body2, joint2->body2)
: Joint(joint1->body2, joint2->body2), joint(NULL)
{
b2GearJointDef def;
def.joint1 = joint1->joint;
+2 -1
View File
@@ -117,7 +117,8 @@ namespace box2d
void Joint::destroyJoint(b2Joint * joint)
{
world->world->DestroyJoint(joint);
if (joint != NULL)
world->world->DestroyJoint(joint);
}
} // box2d
+1 -1
View File
@@ -31,7 +31,7 @@ namespace physics
namespace box2d
{
MouseJoint::MouseJoint(Body * body1, float x, float y)
: Joint(body1)
: Joint(body1), joint(NULL)
{
b2MouseJointDef def;
+1 -1
View File
@@ -36,8 +36,8 @@ namespace box2d
for(int i = 0; i<def->vertexCount; i++)
def->vertices[i] = body->world->scaleDown(def->vertices[i]);
def->userData = (void*)data;
shape = body->body->CreateShape(def);
shape->SetUserData((void*)data);
}
PolygonShape::~PolygonShape()
+1 -1
View File
@@ -31,7 +31,7 @@ namespace physics
namespace box2d
{
PrismaticJoint::PrismaticJoint(Body * body1, Body * body2, float x, float y, float ax, float ay)
: Joint(body1, body2)
: Joint(body1, body2), joint(NULL)
{
b2PrismaticJointDef def;
+1 -1
View File
@@ -31,7 +31,7 @@ namespace physics
namespace box2d
{
PulleyJoint::PulleyJoint(Body * body1, Body * body2, b2Vec2 groundAnchor1, b2Vec2 groundAnchor2, b2Vec2 anchor1, b2Vec2 anchor2, float ratio)
: Joint(body1, body2)
: Joint(body1, body2), joint(NULL)
{
b2PulleyJointDef def;
def.Initialize(body1->body, body2->body, world->scaleDown(groundAnchor1), world->scaleDown(groundAnchor2), \
+1 -1
View File
@@ -33,7 +33,7 @@ namespace physics
namespace box2d
{
RevoluteJoint::RevoluteJoint(Body * body1, Body * body2, float x, float y)
: Joint(body1, body2)
: Joint(body1, body2), joint(NULL)
{
b2RevoluteJointDef def;
def.Initialize(body1->body, body2->body, world->scaleDown(b2Vec2(x,y)));
+3 -2
View File
@@ -34,7 +34,7 @@ namespace physics
namespace box2d
{
Shape::Shape(Body * body)
: body(body)
: body(body), shape(NULL)
{
body->retain();
data = new shapeudata();
@@ -49,7 +49,8 @@ namespace box2d
delete data;
data = 0;
body->body->DestroyShape(shape);
if (shape)
body->body->DestroyShape(shape);
shape = 0;
body->release();
+2 -2
View File
@@ -96,14 +96,14 @@ namespace box2d
}
World::World(b2AABB aabb)
: meter(DEFAULT_METER)
: meter(DEFAULT_METER), world(NULL)
{
world = new b2World(scaleDown(aabb), b2Vec2(0,0), true);
world->SetContactListener(this);
}
World::World(b2AABB aabb, b2Vec2 gravity, bool sleep, int meter)
: meter(meter)
: meter(meter), world(NULL)
{
world = new b2World(scaleDown(aabb), scaleDown(gravity), sleep);
world->SetContactListener(this);