public override void ExitPhysics() { if (m_inverseModel != null) { Debug.Log("Dispose inverse model " + m_inverseModel.NumBodies); m_inverseModel.Dispose(); } Debug.Log("InverseDynamicsExitPhysics"); //cleanup in the reverse order of creation/initialization //remove the rigidbodies from the dynamics world and delete them if (m_dynamicsWorld == null) { int i; for (i = m_dynamicsWorld.NumConstraints - 1; i >= 0; i--) { TypedConstraint tc = m_dynamicsWorld.GetConstraint(i); m_dynamicsWorld.RemoveConstraint(tc); tc.Dispose(); } for (i = m_dynamicsWorld.NumMultiBodyConstraints - 1; i >= 0; i--) { MultiBodyConstraint mbc = m_dynamicsWorld.GetMultiBodyConstraint(i); m_dynamicsWorld.RemoveMultiBodyConstraint(mbc); mbc.Dispose(); } for (i = m_dynamicsWorld.NumMultibodies - 1; i >= 0; i--) { MultiBody mb = m_dynamicsWorld.GetMultiBody(i); m_dynamicsWorld.RemoveMultiBody(mb); mb.Dispose(); } for (i = m_dynamicsWorld.NumCollisionObjects - 1; i >= 0; i--) { CollisionObject obj = m_dynamicsWorld.CollisionObjectArray[i]; RigidBody body = RigidBody.Upcast(obj); if (body != null && body.MotionState != null) { body.MotionState.Dispose(); } m_dynamicsWorld.RemoveCollisionObject(obj); obj.Dispose(); } } if (m_multiBody != null) { m_multiBody.Dispose(); } //delete collision shapes for (int j = 0; j < CollisionShapes.Count; j++) { CollisionShape shape = CollisionShapes[j]; shape.Dispose(); } CollisionShapes.Clear(); m_dynamicsWorld.Dispose(); m_dynamicsWorld = null; m_solver.Dispose(); m_solver = null; Broadphase.Dispose(); Broadphase = null; Dispatcher.Dispose(); Dispatcher = null; m_pairCache.Dispose(); m_pairCache = null; CollisionConf.Dispose(); CollisionConf = null; Debug.Log("After dispose B"); }