From 63f6d91a1a31a868245bd6b5dd0bd8fe7d5786fb Mon Sep 17 00:00:00 2001 From: PouleyKetchoupp Date: Wed, 20 Oct 2021 10:39:16 -0700 Subject: [PATCH] Fix SoftBody memory corruption when using invalid mesh In case of failure the new soft body wasn't created but the previous one wasn't destroyed, causing discrepancies with visual server updates. --- modules/bullet/soft_body_bullet.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/modules/bullet/soft_body_bullet.cpp b/modules/bullet/soft_body_bullet.cpp index d4b850e938b..13daec699ef 100644 --- a/modules/bullet/soft_body_bullet.cpp +++ b/modules/bullet/soft_body_bullet.cpp @@ -117,19 +117,17 @@ void SoftBodyBullet::update_visual_server(SoftBodyVisualServerHandler *p_visual_ } void SoftBodyBullet::set_soft_mesh(const Ref &p_mesh) { - if (p_mesh.is_null()) { - soft_mesh.unref(); - } else { - soft_mesh = p_mesh; - } + destroy_soft_body(); + + soft_mesh = p_mesh; if (soft_mesh.is_null()) { - destroy_soft_body(); return; } - Array arrays = soft_mesh->surface_get_arrays(0); ERR_FAIL_COND(!(soft_mesh->surface_get_format(0) & VS::ARRAY_FORMAT_INDEX)); + + Array arrays = soft_mesh->surface_get_arrays(0); set_trimesh_body_shape(arrays[VS::ARRAY_INDEX], arrays[VS::ARRAY_VERTEX]); }