Browse Source

Merge pull request #46474 from RootKiller/fix_joints

Fix for crash after joint connected node is set to null
Rémi Verschelde 4 years ago
parent
commit
870de12111

+ 0 - 19
servers/physics_2d/joints_2d_sw.cpp

@@ -205,15 +205,6 @@ PinJoint2DSW::PinJoint2DSW(const Vector2 &p_pos, Body2DSW *p_body_a, Body2DSW *p
 	}
 }
 
-PinJoint2DSW::~PinJoint2DSW() {
-	if (A) {
-		A->remove_constraint(this, 0);
-	}
-	if (B) {
-		B->remove_constraint(this, 1);
-	}
-}
-
 //////////////////////////////////////////////
 //////////////////////////////////////////////
 //////////////////////////////////////////////
@@ -346,11 +337,6 @@ GrooveJoint2DSW::GrooveJoint2DSW(const Vector2 &p_a_groove1, const Vector2 &p_a_
 	B->add_constraint(this, 1);
 }
 
-GrooveJoint2DSW::~GrooveJoint2DSW() {
-	A->remove_constraint(this, 0);
-	B->remove_constraint(this, 1);
-}
-
 //////////////////////////////////////////////
 //////////////////////////////////////////////
 //////////////////////////////////////////////
@@ -442,8 +428,3 @@ DampedSpringJoint2DSW::DampedSpringJoint2DSW(const Vector2 &p_anchor_a, const Ve
 	A->add_constraint(this, 0);
 	B->add_constraint(this, 1);
 }
-
-DampedSpringJoint2DSW::~DampedSpringJoint2DSW() {
-	A->remove_constraint(this, 0);
-	B->remove_constraint(this, 1);
-}

+ 9 - 3
servers/physics_2d/joints_2d_sw.h

@@ -60,6 +60,15 @@ public:
 		bias = 0;
 		max_force = max_bias = 3.40282e+38;
 	};
+
+	virtual ~Joint2DSW() {
+		for (int i = 0; i < get_body_count(); i++) {
+			Body2DSW *body = get_body_ptr()[i];
+			if (body) {
+				body->remove_constraint(this, i);
+			}
+		}
+	};
 };
 
 class PinJoint2DSW : public Joint2DSW {
@@ -90,7 +99,6 @@ public:
 	real_t get_param(PhysicsServer2D::PinJointParam p_param) const;
 
 	PinJoint2DSW(const Vector2 &p_pos, Body2DSW *p_body_a, Body2DSW *p_body_b = nullptr);
-	~PinJoint2DSW();
 };
 
 class GrooveJoint2DSW : public Joint2DSW {
@@ -124,7 +132,6 @@ public:
 	virtual void solve(real_t p_step);
 
 	GrooveJoint2DSW(const Vector2 &p_a_groove1, const Vector2 &p_a_groove2, const Vector2 &p_b_anchor, Body2DSW *p_body_a, Body2DSW *p_body_b);
-	~GrooveJoint2DSW();
 };
 
 class DampedSpringJoint2DSW : public Joint2DSW {
@@ -160,7 +167,6 @@ public:
 	real_t get_param(PhysicsServer2D::DampedSpringParam p_param) const;
 
 	DampedSpringJoint2DSW(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, Body2DSW *p_body_a, Body2DSW *p_body_b);
-	~DampedSpringJoint2DSW();
 };
 
 #endif // JOINTS_2D_SW_H

+ 9 - 0
servers/physics_3d/joints_3d_sw.h

@@ -49,6 +49,15 @@ public:
 	_FORCE_INLINE_ Joint3DSW(Body3DSW **p_body_ptr = nullptr, int p_body_count = 0) :
 			Constraint3DSW(p_body_ptr, p_body_count) {
 	}
+
+	virtual ~Joint3DSW() {
+		for (int i = 0; i < get_body_count(); i++) {
+			Body3DSW *body = get_body_ptr()[i];
+			if (body) {
+				body->remove_constraint(this);
+			}
+		}
+	}
 };
 
 #endif // JOINTS_SW_H

+ 0 - 3
servers/physics_3d/physics_server_3d_sw.cpp

@@ -1312,9 +1312,6 @@ void PhysicsServer3DSW::free(RID p_rid) {
 	} else if (joint_owner.owns(p_rid)) {
 		Joint3DSW *joint = joint_owner.getornull(p_rid);
 
-		for (int i = 0; i < joint->get_body_count(); i++) {
-			joint->get_body_ptr()[i]->remove_constraint(joint);
-		}
 		joint_owner.free(p_rid);
 		memdelete(joint);