Fix #108198: Particle hair crash when using Hair Dynamics

`hair_out_mesh` and `hair_in_mesh` implicitly share edges.
In `hair_create_input_mesh()`, edge data of `hair_in_mesh` needs to be
updated and therefore are copied to a new location. In the subsequent
frames, `psys->clmd->clothObject->edges` won't be updated and point to
freed memory block. Therefore, Blender crashes.
By freeing `hair_out_mesh` first,
1. in `hair_create_input_mesh(),` at least edge data copying is avoided
2. `psys->clmd->clothObject->edges` always points to correct memory

However, since it's possible that similar situation will happen again
by adding another strong user to the same `CustomData` in the future,
it is safer to update `psys->clmd->clothObject->edges` for every frame.

Pull Request: https://projects.blender.org/blender/blender/pulls/108480
This commit is contained in:
Chao Li
2023-06-06 18:46:35 +02:00
committed by Hans Goudey
parent d9dfa782eb
commit a56df90931
2 changed files with 8 additions and 2 deletions

View File

@@ -367,6 +367,10 @@ void clothModifier_do(ClothModifierData *clmd,
return;
}
/* Since implicit sharing is introduced, mesh data can be moved to other places.
* Therefore some fields in simulation data need to be updated accordingly */
clmd->clothObject->edges = reinterpret_cast<const vec2i *>(mesh->edges().data());
/* try to read from cache */
bool can_simulate = (framenr == clmd->clothObject->last_frame + 1) &&
!(cache->flag & PTCACHE_BAKED);

View File

@@ -3491,12 +3491,14 @@ static void do_hair_dynamics(ParticleSimulationData *sim)
}
}
hair_create_input_mesh(sim, totpoint, totedge, &psys->hair_in_mesh);
/* Free hair_out_mesh before modifying hair_in_mesh in hair_create_input_mesh() to avoid copying
* on write since they share some data */
if (psys->hair_out_mesh) {
BKE_id_free(NULL, psys->hair_out_mesh);
}
hair_create_input_mesh(sim, totpoint, totedge, &psys->hair_in_mesh);
psys->clmd->point_cache = psys->pointcache;
/* for hair sim we replace the internal cloth effector weights temporarily
* to use the particle settings