Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid retaining ownership of the rcl context #946

Merged
merged 4 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion rclcpp/include/rclcpp/graph_listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ class GraphListener : public std::enable_shared_from_this<GraphListener>
std::vector<rclcpp::node_interfaces::NodeGraphInterface *> node_graph_interfaces_;

rcl_guard_condition_t interrupt_guard_condition_ = rcl_get_zero_initialized_guard_condition();
std::shared_ptr<rcl_context_t> interrupt_guard_condition_context_;
rcl_guard_condition_t * shutdown_guard_condition_;
rcl_wait_set_t wait_set_ = rcl_get_zero_initialized_wait_set();
};
Expand Down
10 changes: 5 additions & 5 deletions rclcpp/src/rclcpp/graph_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,15 @@ GraphListener::GraphListener(std::shared_ptr<rclcpp::Context> parent_context)
: parent_context_(parent_context),
is_started_(false),
is_shutdown_(false),
interrupt_guard_condition_context_(nullptr),
shutdown_guard_condition_(nullptr)
{
// TODO(wjwwood): make a guard condition class in rclcpp so this can be tracked
wjwwood marked this conversation as resolved.
Show resolved Hide resolved
// automatically with the rcl guard condition
// hold on to this context to prevent it from going out of scope while this
// guard condition is using it.
interrupt_guard_condition_context_ = parent_context->get_rcl_context();
rcl_ret_t ret = rcl_guard_condition_init(
&interrupt_guard_condition_,
interrupt_guard_condition_context_.get(),
parent_context->get_rcl_context().get(),
rcl_guard_condition_get_default_options());
if (RCL_RET_OK != ret) {
throw_from_rcl_error(ret, "failed to create interrupt guard condition");
Expand All @@ -72,6 +70,7 @@ GraphListener::start_if_not_started()
}
if (!is_started_) {
// Initialize the wait set before starting.
auto parent_context = parent_context_.lock();
rcl_ret_t ret = rcl_wait_set_init(
&wait_set_,
0, // number_of_subscriptions
Expand All @@ -80,7 +79,7 @@ GraphListener::start_if_not_started()
0, // number_of_clients
0, // number_of_services
0, // number_of_events
interrupt_guard_condition_context_.get(),
parent_context->get_rcl_context().get(),
wjwwood marked this conversation as resolved.
Show resolved Hide resolved
rcl_get_default_allocator());
if (RCL_RET_OK != ret) {
throw_from_rcl_error(ret, "failed to initialize wait set");
Expand Down Expand Up @@ -141,6 +140,8 @@ GraphListener::run_loop()
}
// This lock is released when the loop continues or exits.
std::lock_guard<std::mutex> nodes_lock(node_graph_interfaces_mutex_, std::adopt_lock);
// Ensure that the context doesn't go out of scope.
auto parent_context = parent_context_.lock();

// Resize the wait set if necessary.
const size_t node_graph_interfaces_size = node_graph_interfaces_.size();
Expand Down Expand Up @@ -350,7 +351,6 @@ GraphListener::__shutdown(bool should_throw)
listener_thread_.join();
}
rcl_ret_t ret = rcl_guard_condition_fini(&interrupt_guard_condition_);
interrupt_guard_condition_context_.reset(); // release context guard condition was using
if (RCL_RET_OK != ret) {
throw_from_rcl_error(ret, "failed to finalize interrupt guard condition");
}
Expand Down