Use weak_ptr to store context in GraphListener (#906)

* Use weak_ptr to store context in graph listener

Signed-off-by: Barry Xu <Barry.Xu@sony.com>
This commit is contained in:
Barry Xu 2019-12-03 05:38:41 +08:00 committed by Ivan Santiago Paunovic
parent 5867e52d68
commit 3288bdd2c5
2 changed files with 19 additions and 5 deletions

View file

@ -165,7 +165,7 @@ private:
void void
__shutdown(bool); __shutdown(bool);
rclcpp::Context::SharedPtr parent_context_; rclcpp::Context::WeakPtr parent_context_;
std::thread listener_thread_; std::thread listener_thread_;
bool is_started_; bool is_started_;

View file

@ -80,7 +80,7 @@ GraphListener::start_if_not_started()
0, // number_of_clients 0, // number_of_clients
0, // number_of_services 0, // number_of_services
0, // number_of_events 0, // number_of_events
this->parent_context_->get_rcl_context().get(), interrupt_guard_condition_context_.get(),
rcl_get_default_allocator()); rcl_get_default_allocator());
if (RCL_RET_OK != ret) { if (RCL_RET_OK != ret) {
throw_from_rcl_error(ret, "failed to initialize wait set"); throw_from_rcl_error(ret, "failed to initialize wait set");
@ -355,10 +355,24 @@ GraphListener::__shutdown(bool should_throw)
throw_from_rcl_error(ret, "failed to finalize interrupt guard condition"); throw_from_rcl_error(ret, "failed to finalize interrupt guard condition");
} }
if (shutdown_guard_condition_) { if (shutdown_guard_condition_) {
if (should_throw) { auto parent_context_ptr = parent_context_.lock();
parent_context_->release_interrupt_guard_condition(&wait_set_); if (parent_context_ptr) {
if (should_throw) {
parent_context_ptr->release_interrupt_guard_condition(&wait_set_);
} else {
parent_context_ptr->release_interrupt_guard_condition(&wait_set_, std::nothrow);
}
} else { } else {
parent_context_->release_interrupt_guard_condition(&wait_set_, std::nothrow); ret = rcl_guard_condition_fini(shutdown_guard_condition_);
if (RCL_RET_OK != ret) {
if (should_throw) {
throw_from_rcl_error(ret, "failed to finalize shutdown guard condition");
} else {
RCLCPP_ERROR(
rclcpp::get_logger("rclcpp"),
"failed to finalize shutdown guard condition");
}
}
} }
shutdown_guard_condition_ = nullptr; shutdown_guard_condition_ = nullptr;
} }