check for nullptr to avoid crashes

This commit is contained in:
Andrew Meadows 2016-02-11 13:53:01 -08:00
parent 894c75f8fc
commit 2b91a31919

View file

@ -145,27 +145,30 @@ void PhysicsEngine::removeObjects(const VectorOfMotionStates& objects) {
// then remove them // then remove them
for (auto object : objects) { for (auto object : objects) {
btRigidBody* body = object->getRigidBody(); btRigidBody* body = object->getRigidBody();
assert(body); if (body) {
_dynamicsWorld->removeRigidBody(body); _dynamicsWorld->removeRigidBody(body);
// NOTE: setRigidBody() modifies body->m_userPointer so we should clear the MotionState's body BEFORE deleting it. // NOTE: setRigidBody() modifies body->m_userPointer so we should clear the MotionState's body BEFORE deleting it.
object->setRigidBody(nullptr); object->setRigidBody(nullptr);
body->setMotionState(nullptr); body->setMotionState(nullptr);
delete body; delete body;
}
} }
} }
// Same as above, but takes a Set instead of a Vector. Should only be called during teardown. // Same as above, but takes a Set instead of a Vector. Should only be called during teardown.
void PhysicsEngine::removeObjects(const SetOfMotionStates& objects) { void PhysicsEngine::removeObjects(const SetOfMotionStates& objects) {
_contactMap.clear();
for (auto object : objects) { for (auto object : objects) {
btRigidBody* body = object->getRigidBody(); btRigidBody* body = object->getRigidBody();
assert(body); if (body) {
_dynamicsWorld->removeRigidBody(body); _dynamicsWorld->removeRigidBody(body);
// NOTE: setRigidBody() modifies body->m_userPointer so we should clear the MotionState's body BEFORE deleting it. // NOTE: setRigidBody() modifies body->m_userPointer so we should clear the MotionState's body BEFORE deleting it.
object->setRigidBody(nullptr); object->setRigidBody(nullptr);
body->setMotionState(nullptr); body->setMotionState(nullptr);
delete body; delete body;
}
} }
} }
@ -200,11 +203,12 @@ void PhysicsEngine::reinsertObject(ObjectMotionState* object) {
// remove object from DynamicsWorld // remove object from DynamicsWorld
bumpAndPruneContacts(object); bumpAndPruneContacts(object);
btRigidBody* body = object->getRigidBody(); btRigidBody* body = object->getRigidBody();
assert(body); if (body) {
_dynamicsWorld->removeRigidBody(body); _dynamicsWorld->removeRigidBody(body);
// add it back // add it back
addObjectToDynamicsWorld(object); addObjectToDynamicsWorld(object);
}
} }
void PhysicsEngine::removeContacts(ObjectMotionState* motionState) { void PhysicsEngine::removeContacts(ObjectMotionState* motionState) {