From c73f82b6f07c76705be52d7bb40ee866aea98b9c Mon Sep 17 00:00:00 2001 From: Mitchell Stokes Date: Mon, 18 Nov 2013 09:30:46 -0800 Subject: [PATCH] Fix T37040: Removing vehicles in BGE causes a crash The vehicle constraint is now properly removed if bge.constraints.removeConstraint() is used or the object is deleted. This also fixes a memory leak with the vehicle wrapper. --- .../Physics/Bullet/CcdPhysicsEnvironment.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp b/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp index 71ed6af2f99..cddc12cc754 100644 --- a/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp +++ b/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp @@ -83,6 +83,11 @@ public: { } + ~WrapperVehicle() + { + delete m_vehicle; + } + btRaycastVehicle* GetVehicle() { return m_vehicle; @@ -440,6 +445,19 @@ bool CcdPhysicsEnvironment::RemoveCcdPhysicsController(CcdPhysicsController* ctr //delete con; //might be kept by python KX_ConstraintWrapper } m_dynamicsWorld->removeRigidBody(ctrl->GetRigidBody()); + + // Handle potential vehicle constraints + int numVehicles = m_wrapperVehicles.size(); + int vehicle_constraint = 0; + for (int i=0;iGetChassis() == ctrl) + vehicle_constraint = wrapperVehicle->GetVehicle()->getUserConstraintId(); + } + + if (vehicle_constraint > 0) + RemoveConstraint(vehicle_constraint); } else { //if a softbody @@ -984,6 +1002,14 @@ void CcdPhysicsEnvironment::RemoveConstraint(int constraintId) break; } } + + WrapperVehicle *vehicle; + if ((vehicle = (WrapperVehicle*)GetVehicleConstraint(constraintId))) + { + m_dynamicsWorld->removeVehicle(vehicle->GetVehicle()); + m_wrapperVehicles.erase(std::remove(m_wrapperVehicles.begin(), m_wrapperVehicles.end(), vehicle)); + delete vehicle; + } }