Fix T84397: Creating and removing many objects very quickly causes a crash

The root of the issue was caused by the dependency graph using ID pointer
to map evaluated state from old depsgraph to new one upon relations update.
This was failing when IDs were re-allocated rapidly: was possible that
Object ID's evaluated state assigned to Mesh and vice versa.

Now depsgraph uses Session UUID to identify which IDs to restore evaluated
state to. The session UUID is stored in the IDNode, so that id_orig is not
dereferenced on depsgraph update since the ID might be freed.

The root of the issue is identified by Campbell, original patch was done
by Bastien, thanks! Also thanks to Oliver and Ray and everyone else for
testing!
This commit is contained in:
Sergey Sharybin 2021-01-12 17:12:27 +01:00
parent 957e292c58
commit abbc43e4e4
Notes: blender-bot 2023-02-14 02:27:51 +01:00
Referenced by issue #84789, Crash when switching away from Blender and back again
Referenced by issue #84397, Creating and removing many objects very quickly causes a crash
Referenced by issue #83216, Potential candidates for corrective releases
Referenced by issue #80203, Crash when changing torus properties
Referenced by issue #77348, Blender LTS: Maintenance Task 2.83
4 changed files with 20 additions and 5 deletions

View File

@ -83,6 +83,7 @@
#include "BKE_key.h"
#include "BKE_lattice.h"
#include "BKE_layer.h"
#include "BKE_lib_id.h"
#include "BKE_light.h"
#include "BKE_mask.h"
#include "BKE_material.h"
@ -152,12 +153,14 @@ DepsgraphNodeBuilder::~DepsgraphNodeBuilder()
IDNode *DepsgraphNodeBuilder::add_id_node(ID *id)
{
BLI_assert(id->session_uuid != MAIN_ID_SESSION_UUID_UNSET);
IDNode *id_node = nullptr;
ID *id_cow = nullptr;
IDComponentsMask previously_visible_components_mask = 0;
uint32_t previous_eval_flags = 0;
DEGCustomDataMeshMasks previous_customdata_masks;
IDInfo *id_info = id_info_hash_.lookup_default(id, nullptr);
IDInfo *id_info = id_info_hash_.lookup_default(id->session_uuid, nullptr);
if (id_info != nullptr) {
id_cow = id_info->id_cow;
previously_visible_components_mask = id_info->previously_visible_components_mask;
@ -334,7 +337,8 @@ void DepsgraphNodeBuilder::begin_build()
id_info->previously_visible_components_mask = id_node->visible_components_mask;
id_info->previous_eval_flags = id_node->eval_flags;
id_info->previous_customdata_masks = id_node->customdata_masks;
id_info_hash_.add_new(id_node->id_orig, id_info);
BLI_assert(!id_info_hash_.contains(id_node->id_orig_session_uuid));
id_info_hash_.add_new(id_node->id_orig_session_uuid, id_info);
id_node->id_cow = nullptr;
}

View File

@ -285,8 +285,8 @@ class DepsgraphNodeBuilder : public DepsgraphBuilder {
* very root is visible (aka not restricted.). */
bool is_parent_collection_visible_;
/* Indexed by original ID, values are IDInfo. */
Map<const ID *, IDInfo *> id_info_hash_;
/* Indexed by original ID.session_uuid, values are IDInfo. */
Map<uint, IDInfo *> id_info_hash_;
/* Set of IDs which were already build. Makes it easier to keep track of
* what was already built and what was not. */

View File

@ -80,6 +80,7 @@ void IDNode::init(const ID *id, const char *UNUSED(subdata))
/* Store ID-pointer. */
id_type = GS(id->name);
id_orig = (ID *)id;
id_orig_session_uuid = id->session_uuid;
eval_flags = 0;
previous_eval_flags = 0;
customdata_masks = DEGCustomDataMeshMasks();

View File

@ -74,12 +74,22 @@ struct IDNode : public Node {
IDComponentsMask get_visible_components_mask() const;
/* ID Block referenced. */
/* Type of the ID stored separately, so it's possible to perform check whether CoW is needed
* without de-referencing the id_cow (which is not safe when ID is NOT covered by CoW and has
* been deleted from the main database.) */
ID_Type id_type;
/* ID Block referenced. */
ID *id_orig;
/* Session-wide UUID of the id_orig.
* Is used on relations update to map evaluated state from old nodes to the new ones, without
* relying on pointers (which are not guaranteed to be unique) and without dereferencing id_orig
* which could be "stale" pointer. */
uint id_orig_session_uuid;
/* Evaluated datablock.
* Will be covered by the copy-on-write system if the ID Type needs it. */
ID *id_cow;
/* Hash to make it faster to look up components. */