From a1fccdb17711897aa37385e471482b3e2e342a68 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Tue, 5 Aug 2014 08:32:14 -0700 Subject: [PATCH] remove dangling shape pointers from PhysicsSimulation --- libraries/shared/src/PhysicsEntity.cpp | 3 +++ libraries/shared/src/PhysicsSimulation.cpp | 6 +++++- libraries/shared/src/PhysicsSimulation.h | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/libraries/shared/src/PhysicsEntity.cpp b/libraries/shared/src/PhysicsEntity.cpp index 37d1a88d67..09b00c201c 100644 --- a/libraries/shared/src/PhysicsEntity.cpp +++ b/libraries/shared/src/PhysicsEntity.cpp @@ -65,6 +65,9 @@ void PhysicsEntity::setEnableShapes(bool enable) { } void PhysicsEntity::clearShapes() { + if (_simulation) { + _simulation->removeShapes(this); + } for (int i = 0; i < _shapes.size(); ++i) { delete _shapes[i]; } diff --git a/libraries/shared/src/PhysicsSimulation.cpp b/libraries/shared/src/PhysicsSimulation.cpp index bac4f2ad77..c2a852c32b 100644 --- a/libraries/shared/src/PhysicsSimulation.cpp +++ b/libraries/shared/src/PhysicsSimulation.cpp @@ -70,6 +70,7 @@ void PhysicsSimulation::removeEntity(PhysicsEntity* entity) { if (!entity || !entity->_simulation || !(entity->_simulation == this)) { return; } + removeShapes(entity); int numEntities = _entities.size(); for (int i = 0; i < numEntities; ++i) { if (entity == _entities.at(i)) { @@ -86,7 +87,10 @@ void PhysicsSimulation::removeEntity(PhysicsEntity* entity) { break; } } - // remove corresponding contacts +} + +void PhysicsSimulation::removeShapes(const PhysicsEntity* entity) { + // remove data structures with pointers to entity's shapes QMap::iterator itr = _contacts.begin(); while (itr != _contacts.end()) { if (entity == itr.value().getShapeA()->getEntity() || entity == itr.value().getShapeB()->getEntity()) { diff --git a/libraries/shared/src/PhysicsSimulation.h b/libraries/shared/src/PhysicsSimulation.h index fc6d518d62..506b37533a 100644 --- a/libraries/shared/src/PhysicsSimulation.h +++ b/libraries/shared/src/PhysicsSimulation.h @@ -32,6 +32,7 @@ public: bool addEntity(PhysicsEntity* entity); void removeEntity(PhysicsEntity* entity); + void removeShapes(const PhysicsEntity* entity); /// \return true if doll was added to or is already in the list bool addRagdoll(Ragdoll* doll);