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).
This commit is contained in:
@@ -379,6 +379,8 @@ namespace robot
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Keep owned node copies: yaml-cpp iterator proxies make &it->second dangling.
|
||||||
|
std::vector<YAML::Node> owned;
|
||||||
const YAML::Node *current = &root_;
|
const YAML::Node *current = &root_;
|
||||||
|
|
||||||
for (size_t i = 0; i < segments.size(); ++i)
|
for (size_t i = 0; i < segments.size(); ++i)
|
||||||
@@ -391,8 +393,8 @@ namespace robot
|
|||||||
|
|
||||||
if (current->IsMap())
|
if (current->IsMap())
|
||||||
{
|
{
|
||||||
// Search for the segment without inserting default nodes
|
YAML::Node found_child;
|
||||||
const YAML::Node *found_child = nullptr;
|
bool found = false;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -400,52 +402,46 @@ namespace robot
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Safety check: validate iterator nodes
|
const auto kv = *it;
|
||||||
if (!it->first.IsDefined())
|
if (!kv.first.IsDefined())
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Safely convert key to string
|
|
||||||
std::string key_str;
|
std::string key_str;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
key_str = it->first.as<std::string>();
|
key_str = kv.first.as<std::string>();
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
// Skip nodes that can't be converted to string
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key_str == segments[i])
|
if (key_str == segments[i] && kv.second.IsDefined())
|
||||||
{
|
{
|
||||||
// Safety check: validate value node
|
found_child = kv.second;
|
||||||
if (it->second.IsDefined())
|
found = true;
|
||||||
{
|
break;
|
||||||
found_child = &(it->second);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
// Skip invalid nodes and continue iteration
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
// If iteration fails, return false
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!found_child || !found_child->IsDefined())
|
if (!found || !found_child.IsDefined())
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
current = found_child;
|
owned.push_back(std::move(found_child));
|
||||||
|
current = &owned.back();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -459,12 +455,13 @@ namespace robot
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const YAML::Node &child = (*current)[idx];
|
YAML::Node child = (*current)[idx];
|
||||||
if (!child.IsDefined())
|
if (!child.IsDefined())
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
current = &child;
|
owned.push_back(std::move(child));
|
||||||
|
current = &owned.back();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
@@ -515,7 +512,8 @@ namespace robot
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Traverse the tree without modifying node_handle_
|
// 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<YAML::Node> owned;
|
||||||
const YAML::Node *current = &root_;
|
const YAML::Node *current = &root_;
|
||||||
|
|
||||||
for (size_t i = 0; i < segments.size(); ++i)
|
for (size_t i = 0; i < segments.size(); ++i)
|
||||||
@@ -527,7 +525,8 @@ namespace robot
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Access child node without mutating the map (avoid operator[] default insertion)
|
// 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
|
try
|
||||||
{
|
{
|
||||||
@@ -535,54 +534,47 @@ namespace robot
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Safety check: validate iterator nodes
|
const auto kv = *it;
|
||||||
if (!it->first.IsDefined())
|
if (!kv.first.IsDefined())
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Safely convert key to string
|
|
||||||
std::string key_str;
|
std::string key_str;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
key_str = it->first.as<std::string>();
|
key_str = kv.first.as<std::string>();
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
// Skip nodes that can't be converted to string
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key_str == segments[i])
|
if (key_str == segments[i] && kv.second.IsDefined())
|
||||||
{
|
{
|
||||||
// Safety check: validate value node
|
found_child = kv.second;
|
||||||
if (it->second.IsDefined())
|
found = true;
|
||||||
{
|
break;
|
||||||
found_child = &(it->second);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
// Skip invalid nodes and continue iteration
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
// If iteration fails, return empty node
|
|
||||||
return YAML::Node();
|
return YAML::Node();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!found_child || !found_child->IsDefined())
|
if (!found || !found_child.IsDefined())
|
||||||
{
|
{
|
||||||
return YAML::Node();
|
return YAML::Node();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move to next level
|
owned.push_back(std::move(found_child));
|
||||||
current = found_child;
|
current = &owned.back();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Safety check: ensure current is valid before cloning
|
// Safety check: ensure current is valid before cloning
|
||||||
@@ -629,6 +621,7 @@ namespace robot
|
|||||||
return YAML::Node();
|
return YAML::Node();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<YAML::Node> owned;
|
||||||
const YAML::Node *current = &root_;
|
const YAML::Node *current = &root_;
|
||||||
|
|
||||||
for (size_t i = 0; i < segments.size(); ++i)
|
for (size_t i = 0; i < segments.size(); ++i)
|
||||||
@@ -643,9 +636,10 @@ namespace robot
|
|||||||
bool found_child = false;
|
bool found_child = false;
|
||||||
for (auto it = current->begin(); it != current->end(); ++it)
|
for (auto it = current->begin(); it != current->end(); ++it)
|
||||||
{
|
{
|
||||||
if (it->first.as<std::string>() == segments[i])
|
const auto kv = *it;
|
||||||
|
if (kv.first.as<std::string>() == segments[i])
|
||||||
{
|
{
|
||||||
segment_node = it->second;
|
segment_node = kv.second;
|
||||||
found_child = true;
|
found_child = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -659,7 +653,8 @@ namespace robot
|
|||||||
return YAML::Node();
|
return YAML::Node();
|
||||||
}
|
}
|
||||||
|
|
||||||
current = &segment_node;
|
owned.push_back(std::move(segment_node));
|
||||||
|
current = &owned.back();
|
||||||
}
|
}
|
||||||
return YAML::Clone(*current);
|
return YAML::Clone(*current);
|
||||||
}
|
}
|
||||||
@@ -712,8 +707,10 @@ namespace robot
|
|||||||
{
|
{
|
||||||
for (auto it = node.begin(); it != node.end(); ++it)
|
for (auto it = node.begin(); it != node.end(); ++it)
|
||||||
{
|
{
|
||||||
std::string key = it->first.as<std::string>();
|
// Copy pair: it->second is a temporary (yaml-cpp iterator proxy).
|
||||||
const YAML::Node &value = it->second;
|
const auto kv = *it;
|
||||||
|
std::string key = kv.first.as<std::string>();
|
||||||
|
const YAML::Node value = kv.second;
|
||||||
std::string full_key = prefix.empty() ? key : prefix + "/" + key;
|
std::string full_key = prefix.empty() ? key : prefix + "/" + key;
|
||||||
if(full_key.find("MKTAlgorithmDiffPredictiveTrajectory") == std::string::npos)
|
if(full_key.find("MKTAlgorithmDiffPredictiveTrajectory") == std::string::npos)
|
||||||
continue;
|
continue;
|
||||||
@@ -803,8 +800,12 @@ namespace robot
|
|||||||
|
|
||||||
for (auto it = src.begin(); it != src.end(); ++it)
|
for (auto it = src.begin(); it != src.end(); ++it)
|
||||||
{
|
{
|
||||||
std::string key = it->first.as<std::string>();
|
// yaml-cpp map iterators return a temporary pair via operator-> proxy.
|
||||||
const YAML::Node &src_value = it->second;
|
// 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<std::string>();
|
||||||
|
YAML::Node src_value = kv.second;
|
||||||
|
|
||||||
// Get current value in dest (if exists)
|
// Get current value in dest (if exists)
|
||||||
YAML::Node dest_current = dest[key];
|
YAML::Node dest_current = dest[key];
|
||||||
@@ -1879,6 +1880,7 @@ namespace robot
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<YAML::Node> owned;
|
||||||
const YAML::Node *current = search_root;
|
const YAML::Node *current = search_root;
|
||||||
for (const auto &seg : segments)
|
for (const auto &seg : segments)
|
||||||
{
|
{
|
||||||
@@ -1888,7 +1890,8 @@ namespace robot
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const YAML::Node *found_child = nullptr;
|
YAML::Node found_child;
|
||||||
|
bool found = false;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -1896,53 +1899,47 @@ namespace robot
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Safety check: validate iterator nodes
|
const auto kv = *it;
|
||||||
if (!it->first.IsDefined())
|
if (!kv.first.IsDefined())
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Safely convert key to string
|
|
||||||
std::string key_str;
|
std::string key_str;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
key_str = it->first.as<std::string>();
|
key_str = kv.first.as<std::string>();
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
// Skip nodes that can't be converted to string
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key_str == seg)
|
if (key_str == seg && kv.second.IsDefined())
|
||||||
{
|
{
|
||||||
// Safety check: validate value node
|
found_child = kv.second;
|
||||||
if (it->second.IsDefined())
|
found = true;
|
||||||
{
|
break;
|
||||||
found_child = &(it->second);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
// Skip invalid nodes and continue iteration
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
// If iteration fails, return false
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!found_child || !found_child->IsDefined())
|
if (!found || !found_child.IsDefined())
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
current = found_child;
|
owned.push_back(std::move(found_child));
|
||||||
|
current = &owned.back();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Safety check: ensure current is valid before returning
|
// Safety check: ensure current is valid before returning
|
||||||
|
|||||||
@@ -57,6 +57,8 @@ void PluginLoaderHelper::setNodeHandle(robot::NodeHandle nh, const std::string&
|
|||||||
|
|
||||||
std::string PluginLoaderHelper::findLibraryPath(const std::string& symbol_name)
|
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
|
// Build parameter path: {config_namespace}/{symbol_name}/library_path
|
||||||
std::string param_path;
|
std::string param_path;
|
||||||
if (!config_namespace_.empty()) {
|
if (!config_namespace_.empty()) {
|
||||||
@@ -67,14 +69,32 @@ std::string PluginLoaderHelper::findLibraryPath(const std::string& symbol_name)
|
|||||||
// Try to read from NodeHandle
|
// Try to read from NodeHandle
|
||||||
std::string library_path;
|
std::string library_path;
|
||||||
robot::log_info_at(__FILE__, __LINE__, "%s", symbol_name.c_str());
|
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)) {
|
if (nh_.hasParam(param_path)) {
|
||||||
nh_.getParam(param_path, library_path, std::string(""));
|
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()) {
|
if (!library_path.empty()) {
|
||||||
std::string resolved_path = resolveLibraryPath(library_path);
|
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()) {
|
if (!resolved_path.empty()) {
|
||||||
return resolved_path;
|
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")
|
// 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) {
|
for (const auto& name : possible_names) {
|
||||||
std::filesystem::path full_path = std::filesystem::path(path) / name;
|
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)) {
|
if (std::filesystem::exists(full_path)) {
|
||||||
log_info("[PluginLoaderHelper] Found library in LD_LIBRARY_PATH: %s",
|
log_info("[PluginLoaderHelper] Found library in LD_LIBRARY_PATH: %s",
|
||||||
full_path.string().c_str());
|
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());
|
log_warning("[PluginLoaderHelper] Library not found for symbol: %s", symbol_name.c_str());
|
||||||
@@ -440,4 +467,3 @@ std::string PluginLoaderHelper::getWorkspacePath()
|
|||||||
}
|
}
|
||||||
|
|
||||||
} // namespace robot
|
} // namespace robot
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user