浏览代码

Move removal of the constraints from bodies to Joint3DSW destructor

Joint3DSW instances are also destroyed without explicitly calling free()
for example when changing constrainted bodies at runtime using
set_node method.
Eryk Dwornicki 4 年之前
父节点
当前提交
aa42e8920b
共有 2 个文件被更改,包括 9 次插入3 次删除
  1. 9 0
      servers/physics_3d/joints_3d_sw.h
  2. 0 3
      servers/physics_3d/physics_server_3d_sw.cpp

+ 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) :
 	_FORCE_INLINE_ Joint3DSW(Body3DSW **p_body_ptr = nullptr, int p_body_count = 0) :
 			Constraint3DSW(p_body_ptr, p_body_count) {
 			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
 #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)) {
 	} else if (joint_owner.owns(p_rid)) {
 		Joint3DSW *joint = joint_owner.getornull(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);
 		joint_owner.free(p_rid);
 		memdelete(joint);
 		memdelete(joint);