To reproduce, from startup scene, switch cube to sculpt mode, then edit,
then sculpt again, and undo twice.
Root of the issue is that ED_object_sculptmode_enter_ex frees
sculptsession, then calls sculpt_init_session which ends up using
evaluated object that still has that sculptsession pointer in it, since
depsgraph is not updated at all in the mean time.
This is the simplest fix, however am quiet not sure it is a proper one,
I suspect the issue to go deeper.
Bug comes actually from BKE_sculpt_update_object_for_edit, which calls
mesh_get_eval_final outside of any depsgraph evaluation, and without
even ensuring it is up to date.
I would rather see BKE_object_get_evaluated_mesh being used here? But
code in calling ED_object_sculptmode_enter_ex actually frees object
cache, so that would always return NULL here.