Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions include/network_bridge/network_bridge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ class NetworkBridge : public rclcpp::Node
*/
virtual void initialize();

/**
* @brief Destroy the objects created by the bridge
*
* It should be called once spinning is over, and before the shared pointer is reset, to
* make sure the garbage collector can run
*/
virtual void shutdown();

protected:
/**
* @brief Loads default parameters and creates subsciption managers for each topic.
Expand Down
22 changes: 15 additions & 7 deletions src/network_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,7 @@ NetworkBridge::NetworkBridge(const std::string & node_name)

NetworkBridge::~NetworkBridge()
{
network_interface_->close();
network_interface_.reset();

network_check_timer_.reset();
sub_mgrs_.clear();
timers_.clear();
publishers_.clear();
shutdown();
}

void NetworkBridge::initialize()
Expand All @@ -59,6 +53,19 @@ void NetworkBridge::initialize()
network_interface_->open();
}

void NetworkBridge::shutdown()
{
if (network_interface_) {
network_interface_->close();
}
network_interface_.reset();

network_check_timer_.reset();
sub_mgrs_.clear();
timers_.clear();
publishers_.clear();
}

void NetworkBridge::load_parameters()
{
this->declare_parameter(
Expand Down Expand Up @@ -422,6 +429,7 @@ int main(int argc, char ** argv)
node->initialize();

rclcpp::spin(node);
node->shutdown();
node.reset();

rclcpp::shutdown();
Expand Down
2 changes: 2 additions & 0 deletions src/network_interfaces/tcp_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ void TcpInterface::close()
try {
packet_thread_.join();
} catch (std::system_error &) {
// the thread was not started
}
io_context_.stop();
try {
io_thread_.join();
} catch (std::system_error &) {
// the thread was not started
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/network_interfaces/udp_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ void UdpInterface::open()
void UdpInterface::close()
{
io_context_.stop();
io_thread_.join();
try {
io_thread_.join();
} catch (std::system_error &) {
// the thread was not started
}
}

void UdpInterface::load_parameters()
Expand Down