From a180bed9adae72965f036ad3a0b28605e575e20e Mon Sep 17 00:00:00 2001 From: duongtd Date: Mon, 16 Nov 2026 14:41:30 +0700 Subject: [PATCH] fix(robot_cpp): keep owned YAML node copies while walking param paths yaml-cpp map iterators return the key/value pair through a temporary proxy, so binding a pointer or reference to it->second dangles after the full expression and throws "invalid node" on yaml-cpp 0.9+. Copy the pair and keep owned child nodes alive during traversal. Also add debug logs to PluginLoaderHelper::findLibraryPath for the global_costmap symbol (namespace, param path, library path env, resolved path). --- src/Libraries/robot_cpp/src/node_handle.cpp | 149 +++++++++--------- .../robot_cpp/src/plugin_loader_helper.cpp | 28 +++- 2 files changed, 100 insertions(+), 77 deletions(-) diff --git a/src/Libraries/robot_cpp/src/node_handle.cpp b/src/Libraries/robot_cpp/src/node_handle.cpp index 3345288..3ee0f3f 100644 --- a/src/Libraries/robot_cpp/src/node_handle.cpp +++ b/src/Libraries/robot_cpp/src/node_handle.cpp @@ -379,6 +379,8 @@ namespace robot return false; } + // Keep owned node copies: yaml-cpp iterator proxies make &it->second dangling. + std::vector owned; const YAML::Node *current = &root_; for (size_t i = 0; i < segments.size(); ++i) @@ -391,61 +393,55 @@ namespace robot if (current->IsMap()) { - // Search for the segment without inserting default nodes - const YAML::Node *found_child = nullptr; - + YAML::Node found_child; + bool found = false; + try { for (YAML::const_iterator it = current->begin(); it != current->end(); ++it) { try { - // Safety check: validate iterator nodes - if (!it->first.IsDefined()) + const auto kv = *it; + if (!kv.first.IsDefined()) { continue; } - - // Safely convert key to string + std::string key_str; try { - key_str = it->first.as(); + key_str = kv.first.as(); } catch (...) { - // Skip nodes that can't be converted to string continue; } - - if (key_str == segments[i]) + + if (key_str == segments[i] && kv.second.IsDefined()) { - // Safety check: validate value node - if (it->second.IsDefined()) - { - found_child = &(it->second); - break; - } + found_child = kv.second; + found = true; + break; } } catch (...) { - // Skip invalid nodes and continue iteration continue; } } } catch (...) { - // If iteration fails, return false return false; } - - if (!found_child || !found_child->IsDefined()) + + if (!found || !found_child.IsDefined()) { return false; } - current = found_child; + owned.push_back(std::move(found_child)); + current = &owned.back(); continue; } @@ -459,12 +455,13 @@ namespace robot { return false; } - const YAML::Node &child = (*current)[idx]; + YAML::Node child = (*current)[idx]; if (!child.IsDefined()) { return false; } - current = &child; + owned.push_back(std::move(child)); + current = &owned.back(); continue; } catch (...) @@ -477,7 +474,7 @@ namespace robot // Neither map nor sequence: cannot traverse further return false; } - + // Safety check: ensure current is valid before returning return current && current->IsDefined(); } @@ -515,7 +512,8 @@ namespace robot } // Traverse the tree without modifying node_handle_ - // Use const reference to the original node_handle_ to avoid any modifications + // Owned copies keep child nodes alive (yaml-cpp iterator proxies are temporaries). + std::vector owned; const YAML::Node *current = &root_; for (size_t i = 0; i < segments.size(); ++i) @@ -527,62 +525,56 @@ namespace robot } // Access child node without mutating the map (avoid operator[] default insertion) - const YAML::Node *found_child = nullptr; - + YAML::Node found_child; + bool found = false; + try { for (YAML::const_iterator it = current->begin(); it != current->end(); ++it) { try { - // Safety check: validate iterator nodes - if (!it->first.IsDefined()) + const auto kv = *it; + if (!kv.first.IsDefined()) { continue; } - - // Safely convert key to string + std::string key_str; try { - key_str = it->first.as(); + key_str = kv.first.as(); } catch (...) { - // Skip nodes that can't be converted to string continue; } - - if (key_str == segments[i]) + + if (key_str == segments[i] && kv.second.IsDefined()) { - // Safety check: validate value node - if (it->second.IsDefined()) - { - found_child = &(it->second); - break; - } + found_child = kv.second; + found = true; + break; } } catch (...) { - // Skip invalid nodes and continue iteration continue; } } } catch (...) { - // If iteration fails, return empty node return YAML::Node(); } - - if (!found_child || !found_child->IsDefined()) + + if (!found || !found_child.IsDefined()) { return YAML::Node(); } - // Move to next level - current = found_child; + owned.push_back(std::move(found_child)); + current = &owned.back(); } // Safety check: ensure current is valid before cloning @@ -629,6 +621,7 @@ namespace robot return YAML::Node(); } + std::vector owned; const YAML::Node *current = &root_; for (size_t i = 0; i < segments.size(); ++i) @@ -643,9 +636,10 @@ namespace robot bool found_child = false; for (auto it = current->begin(); it != current->end(); ++it) { - if (it->first.as() == segments[i]) + const auto kv = *it; + if (kv.first.as() == segments[i]) { - segment_node = it->second; + segment_node = kv.second; found_child = true; break; } @@ -659,7 +653,8 @@ namespace robot return YAML::Node(); } - current = &segment_node; + owned.push_back(std::move(segment_node)); + current = &owned.back(); } return YAML::Clone(*current); } @@ -712,8 +707,10 @@ namespace robot { for (auto it = node.begin(); it != node.end(); ++it) { - std::string key = it->first.as(); - const YAML::Node &value = it->second; + // Copy pair: it->second is a temporary (yaml-cpp iterator proxy). + const auto kv = *it; + std::string key = kv.first.as(); + const YAML::Node value = kv.second; std::string full_key = prefix.empty() ? key : prefix + "/" + key; if(full_key.find("MKTAlgorithmDiffPredictiveTrajectory") == std::string::npos) continue; @@ -803,8 +800,12 @@ namespace robot for (auto it = src.begin(); it != src.end(); ++it) { - std::string key = it->first.as(); - const YAML::Node &src_value = it->second; + // yaml-cpp map iterators return a temporary pair via operator-> proxy. + // Binding a reference to it->second dangles after the full-expression and + // throws "invalid node; ... map iterator as a sequence iterator" on 0.9+. + const auto kv = *it; + std::string key = kv.first.as(); + YAML::Node src_value = kv.second; // Get current value in dest (if exists) YAML::Node dest_current = dest[key]; @@ -1879,6 +1880,7 @@ namespace robot return false; } + std::vector owned; const YAML::Node *current = search_root; for (const auto &seg : segments) { @@ -1888,61 +1890,56 @@ namespace robot return false; } - const YAML::Node *found_child = nullptr; - + YAML::Node found_child; + bool found = false; + try { for (YAML::const_iterator it = current->begin(); it != current->end(); ++it) { try { - // Safety check: validate iterator nodes - if (!it->first.IsDefined()) + const auto kv = *it; + if (!kv.first.IsDefined()) { continue; } - - // Safely convert key to string + std::string key_str; try { - key_str = it->first.as(); + key_str = kv.first.as(); } catch (...) { - // Skip nodes that can't be converted to string continue; } - - if (key_str == seg) + + if (key_str == seg && kv.second.IsDefined()) { - // Safety check: validate value node - if (it->second.IsDefined()) - { - found_child = &(it->second); - break; - } + found_child = kv.second; + found = true; + break; } } catch (...) { - // Skip invalid nodes and continue iteration continue; } } } catch (...) { - // If iteration fails, return false return false; } - if (!found_child || !found_child->IsDefined()) + if (!found || !found_child.IsDefined()) { return false; } - current = found_child; + owned.push_back(std::move(found_child)); + current = &owned.back(); } // Safety check: ensure current is valid before returning @@ -2059,4 +2056,4 @@ namespace robot std::string NodeHandle::config_directory_; YAML::Node NodeHandle::root_; -} // namespace robot +} // namespace robot \ No newline at end of file diff --git a/src/Libraries/robot_cpp/src/plugin_loader_helper.cpp b/src/Libraries/robot_cpp/src/plugin_loader_helper.cpp index 7049a61..f48bf5b 100644 --- a/src/Libraries/robot_cpp/src/plugin_loader_helper.cpp +++ b/src/Libraries/robot_cpp/src/plugin_loader_helper.cpp @@ -57,6 +57,8 @@ void PluginLoaderHelper::setNodeHandle(robot::NodeHandle nh, const std::string& std::string PluginLoaderHelper::findLibraryPath(const std::string& symbol_name) { + const bool dbg = (symbol_name == "global_costmap"); + // Build parameter path: {config_namespace}/{symbol_name}/library_path std::string param_path; if (!config_namespace_.empty()) { @@ -67,14 +69,32 @@ std::string PluginLoaderHelper::findLibraryPath(const std::string& symbol_name) // Try to read from NodeHandle std::string library_path; robot::log_info_at(__FILE__, __LINE__, "%s", symbol_name.c_str()); + if (dbg) { + const char* nav_lib = std::getenv("PNKX_NAV_CORE_LIBRARY_PATH"); + const char* ld_env = std::getenv("LD_LIBRARY_PATH"); + log_debug("[PluginLoaderHelper][global_costmap] nh_ns='%s' config_ns='%s' param_path='%s' " + "PNKX_NAV_CORE_LIBRARY_PATH='%s' LD_LIBRARY_PATH='%s'\n", + nh_.getNamespace().c_str(), config_namespace_.c_str(), param_path.c_str(), + nav_lib ? nav_lib : "(null)", ld_env ? ld_env : "(null)"); + } if (nh_.hasParam(param_path)) { nh_.getParam(param_path, library_path, std::string("")); + if (dbg) { + log_debug("[PluginLoaderHelper][global_costmap] hasParam('%s')=true library_path='%s'\n", + param_path.c_str(), library_path.c_str()); + } if (!library_path.empty()) { std::string resolved_path = resolveLibraryPath(library_path); + if (dbg) { + log_debug("[PluginLoaderHelper][global_costmap] resolveLibraryPath('%s') -> '%s'\n", + library_path.c_str(), resolved_path.c_str()); + } if (!resolved_path.empty()) { return resolved_path; } } + } else if (dbg) { + log_debug("[PluginLoaderHelper][global_costmap] hasParam('%s')=false\n", param_path.c_str()); } // Try without namespace in symbol name (e.g., "custom_planner::CustomPlanner" -> "CustomPlanner") @@ -122,6 +142,10 @@ std::string PluginLoaderHelper::findLibraryPath(const std::string& symbol_name) for (const auto& name : possible_names) { std::filesystem::path full_path = std::filesystem::path(path) / name; + if (dbg) { + log_debug("[PluginLoaderHelper][global_costmap] try LD_LIBRARY_PATH '%s'\n", + full_path.string().c_str()); + } if (std::filesystem::exists(full_path)) { log_info("[PluginLoaderHelper] Found library in LD_LIBRARY_PATH: %s", full_path.string().c_str()); @@ -130,6 +154,9 @@ std::string PluginLoaderHelper::findLibraryPath(const std::string& symbol_name) } } } + } else if (dbg) { + log_debug("[PluginLoaderHelper][global_costmap] LD_LIBRARY_PATH unset; library_path still='%s'\n", + library_path.c_str()); } log_warning("[PluginLoaderHelper] Library not found for symbol: %s", symbol_name.c_str()); @@ -440,4 +467,3 @@ std::string PluginLoaderHelper::getWorkspacePath() } } // namespace robot -