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 c473b2ce8b.
This commit is contained in:
Hans Goudey 2022-03-30 20:54:11 -05:00
parent 49858bf526
commit 762d3a48e8
6 changed files with 45 additions and 15 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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)