[Bf-blender-cvs] [762d3a48e86] master: Cleanup: Avoid storing pointers for attribute search callback

Hans Goudey noreply at git.blender.org
Thu Mar 31 03:54:18 CEST 2022


Commit: 762d3a48e860d3425fc45bb0a7d4b86fb8cc836b
Author: Hans Goudey
Date:   Wed Mar 30 20:54:11 2022 -0500
Branches: master
https://developer.blender.org/rB762d3a48e860d3425fc45bb0a7d4b86fb8cc836b

Cleanup: Avoid storing pointers for attribute search callback

It's better to use some local/stable identifiier to avoid relying on
the data not being freed in between creating the search menu and
the exec function. This is similar to c473b2ce8bdbf8fa.

===================================================================

M	source/blender/editors/space_node/drawnode.cc
M	source/blender/editors/space_node/node_geometry_attribute_search.cc
M	source/blender/editors/space_node/node_intern.hh
M	source/blender/editors/space_node/node_templates.cc
M	source/blender/nodes/NOD_geometry_nodes_eval_log.hh
M	source/blender/nodes/intern/geometry_nodes_eval_log.cc

===================================================================

diff --git a/source/blender/editors/space_node/drawnode.cc b/source/blender/editors/space_node/drawnode.cc
index c6839099827..958a9fdfc60 100644
--- a/source/blender/editors/space_node/drawnode.cc
+++ b/source/blender/editors/space_node/drawnode.cc
@@ -1302,8 +1302,7 @@ static void std_node_socket_draw(
       uiItemL(row, text, 0);
 
       if (socket_needs_attribute_search(*node, *sock)) {
-        const bNodeTree *node_tree = (const bNodeTree *)node_ptr->owner_id;
-        node_geometry_add_attribute_search_button(*C, *node_tree, *node, *ptr, *row);
+        node_geometry_add_attribute_search_button(*C, *node, *ptr, *row);
       }
       else {
         uiItemR(row, ptr, "default_value", DEFAULT_FLAGS, "", 0);
diff --git a/source/blender/editors/space_node/node_geometry_attribute_search.cc b/source/blender/editors/space_node/node_geometry_attribute_search.cc
index 5a6fe911a05..4a0f3b9a1f2 100644
--- a/source/blender/editors/space_node/node_geometry_attribute_search.cc
+++ b/source/blender/editors/space_node/node_geometry_attribute_search.cc
@@ -38,9 +38,8 @@ using geo_log::GeometryAttributeInfo;
 namespace blender::ed::space_node {
 
 struct AttributeSearchData {
-  const bNodeTree *tree;
-  const bNode *node;
-  bNodeSocket *socket;
+  char node_name[MAX_NAME];
+  char socket_identifier[MAX_NAME];
 };
 
 /* This class must not have a destructor, since it is used by buttons and freed with #MEM_freeN. */
@@ -57,7 +56,7 @@ static void attribute_search_update_fn(
 
   SpaceNode *snode = CTX_wm_space_node(C);
   const geo_log::NodeLog *node_log = geo_log::ModifierLog::find_node_by_node_editor_context(
-      *snode, *data->node);
+      *snode, data->node_name);
   if (node_log == nullptr) {
     return;
   }
@@ -71,21 +70,40 @@ static void attribute_search_exec_fn(bContext *C, void *data_v, void *item_v)
   if (ED_screen_animation_playing(CTX_wm_manager(C))) {
     return;
   }
-  if (item_v == nullptr) {
+  GeometryAttributeInfo *item = (GeometryAttributeInfo *)item_v;
+  if (item == nullptr) {
+    return;
+  }
+  SpaceNode *snode = CTX_wm_space_node(C);
+  if (!snode) {
+    BLI_assert_unreachable();
+    return;
+  }
+  bNodeTree *node_tree = snode->edittree;
+  if (node_tree == nullptr) {
+    BLI_assert_unreachable();
     return;
   }
   AttributeSearchData *data = static_cast<AttributeSearchData *>(data_v);
-  GeometryAttributeInfo *item = (GeometryAttributeInfo *)item_v;
+  bNode *node = nodeFindNodebyName(node_tree, data->node_name);
+  if (node == nullptr) {
+    BLI_assert_unreachable();
+    return;
+  }
+  bNodeSocket *socket = bke::node_find_enabled_input_socket(*node, data->socket_identifier);
+  if (socket == nullptr) {
+    BLI_assert_unreachable();
+    return;
+  }
+  BLI_assert(socket->type == SOCK_STRING);
 
-  bNodeSocket &socket = *data->socket;
-  bNodeSocketValueString *value = static_cast<bNodeSocketValueString *>(socket.default_value);
+  bNodeSocketValueString *value = static_cast<bNodeSocketValueString *>(socket->default_value);
   BLI_strncpy(value->value, item->name.c_str(), MAX_NAME);
 
   ED_undo_push(C, "Assign Attribute Name");
 }
 
 void node_geometry_add_attribute_search_button(const bContext &UNUSED(C),
-                                               const bNodeTree &node_tree,
                                                const bNode &node,
                                                PointerRNA &socket_ptr,
                                                uiLayout &layout)
@@ -109,8 +127,10 @@ void node_geometry_add_attribute_search_button(const bContext &UNUSED(C),
                                  0.0f,
                                  "");
 
-  AttributeSearchData *data = MEM_new<AttributeSearchData>(
-      __func__, AttributeSearchData{&node_tree, &node, (bNodeSocket *)socket_ptr.data});
+  const bNodeSocket &socket = *static_cast<const bNodeSocket *>(socket_ptr.data);
+  AttributeSearchData *data = MEM_new<AttributeSearchData>(__func__);
+  BLI_strncpy(data->node_name, node.name, sizeof(data->node_name));
+  BLI_strncpy(data->socket_identifier, socket.identifier, sizeof(data->socket_identifier));
 
   UI_but_func_search_set_results_are_suggestions(but, true);
   UI_but_func_search_set_sep_string(but, UI_MENU_ARROW_SEP);
diff --git a/source/blender/editors/space_node/node_intern.hh b/source/blender/editors/space_node/node_intern.hh
index cd40573607d..44163cd5ae3 100644
--- a/source/blender/editors/space_node/node_intern.hh
+++ b/source/blender/editors/space_node/node_intern.hh
@@ -353,7 +353,6 @@ void NODE_GGT_backdrop_corner_pin(wmGizmoGroupType *gzgt);
 /* node_geometry_attribute_search.cc */
 
 void node_geometry_add_attribute_search_button(const bContext &C,
-                                               const bNodeTree &node_tree,
                                                const bNode &node,
                                                PointerRNA &socket_ptr,
                                                uiLayout &layout);
diff --git a/source/blender/editors/space_node/node_templates.cc b/source/blender/editors/space_node/node_templates.cc
index b63cb2eeee5..e92e0571157 100644
--- a/source/blender/editors/space_node/node_templates.cc
+++ b/source/blender/editors/space_node/node_templates.cc
@@ -884,7 +884,7 @@ static void ui_node_draw_input(
           if (node_tree->type == NTREE_GEOMETRY && snode != nullptr) {
             /* Only add the attribute search in the node editor, in other places there is not
              * enough context. */
-            node_geometry_add_attribute_search_button(*C, *node_tree, *node, inputptr, *row);
+            node_geometry_add_attribute_search_button(*C, *node, inputptr, *row);
           }
           else {
             uiItemR(sub, &inputptr, "default_value", 0, "", ICON_NONE);
diff --git a/source/blender/nodes/NOD_geometry_nodes_eval_log.hh b/source/blender/nodes/NOD_geometry_nodes_eval_log.hh
index 319fcdeebb7..19103b82fcf 100644
--- a/source/blender/nodes/NOD_geometry_nodes_eval_log.hh
+++ b/source/blender/nodes/NOD_geometry_nodes_eval_log.hh
@@ -353,6 +353,8 @@ class ModifierLog {
   static const TreeLog *find_tree_by_node_editor_context(const SpaceNode &snode);
   static const NodeLog *find_node_by_node_editor_context(const SpaceNode &snode,
                                                          const bNode &node);
+  static const NodeLog *find_node_by_node_editor_context(const SpaceNode &snode,
+                                                         const StringRef node_name);
   static const SocketLog *find_socket_by_node_editor_context(const SpaceNode &snode,
                                                              const bNode &node,
                                                              const bNodeSocket &socket);
diff --git a/source/blender/nodes/intern/geometry_nodes_eval_log.cc b/source/blender/nodes/intern/geometry_nodes_eval_log.cc
index 5c8f4c52f75..13f38c3352e 100644
--- a/source/blender/nodes/intern/geometry_nodes_eval_log.cc
+++ b/source/blender/nodes/intern/geometry_nodes_eval_log.cc
@@ -337,6 +337,16 @@ const NodeLog *ModifierLog::find_node_by_node_editor_context(const SpaceNode &sn
   return tree_log->lookup_node_log(node);
 }
 
+const NodeLog *ModifierLog::find_node_by_node_editor_context(const SpaceNode &snode,
+                                                             const StringRef node_name)
+{
+  const TreeLog *tree_log = ModifierLog::find_tree_by_node_editor_context(snode);
+  if (tree_log == nullptr) {
+    return nullptr;
+  }
+  return tree_log->lookup_node_log(node_name);
+}
+
 const SocketLog *ModifierLog::find_socket_by_node_editor_context(const SpaceNode &snode,
                                                                  const bNode &node,
                                                                  const bNodeSocket &socket)



More information about the Bf-blender-cvs mailing list