Skip to content

Commit b9a7e74

Browse files
committed
Exception safe cleanup
1 parent ef427a7 commit b9a7e74

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

rclcpp/src/rclcpp/node_interfaces/node_parameters.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "rcl_interfaces/srv/list_parameters.hpp"
2626
#include "rclcpp/create_publisher.hpp"
2727
#include "rclcpp/parameter_map.hpp"
28+
#include "rclcpp/scope_exit.hpp"
2829
#include "rcutils/logging_macros.h"
2930
#include "rmw/qos_profiles.h"
3031

@@ -76,11 +77,16 @@ NodeParameters::NodeParameters(
7677
if (RCL_RET_OK != ret) {
7778
rclcpp::exceptions::throw_from_rcl_error(ret);
7879
}
80+
auto cleanup_param_files = make_scope_exit(
81+
[&param_files, &num_yaml_files, &options]() {
82+
for (int i = 0; i < num_yaml_files; ++i) {
83+
options->allocator.deallocate(param_files[i], options->allocator.state);
84+
}
85+
options->allocator.deallocate(param_files, options->allocator.state);
86+
});
7987
for (int i = 0; i < num_yaml_files; ++i) {
8088
yaml_paths.emplace_back(param_files[i]);
81-
options->allocator.deallocate(param_files[i], options->allocator.state);
8289
}
83-
options->allocator.deallocate(param_files, options->allocator.state);
8490
}
8591
};
8692

0 commit comments

Comments
 (0)