Browse Source

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.
vrld 14 years ago
parent
commit
1c7e34715b

+ 2 - 1
src/modules/physics/box2d/CircleShape.cpp

@@ -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
src/modules/physics/box2d/DistanceJoint.cpp

@@ -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
src/modules/physics/box2d/GearJoint.cpp

@@ -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
src/modules/physics/box2d/Joint.cpp

@@ -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
src/modules/physics/box2d/MouseJoint.cpp

@@ -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
src/modules/physics/box2d/PolygonShape.cpp

@@ -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
src/modules/physics/box2d/PrismaticJoint.cpp

@@ -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
src/modules/physics/box2d/PulleyJoint.cpp

@@ -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
src/modules/physics/box2d/RevoluteJoint.cpp

@@ -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
src/modules/physics/box2d/Shape.cpp

@@ -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
src/modules/physics/box2d/World.cpp

@@ -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);