ROS2-001-What do initialisation and resource release actually do in a program?
9/12/24...Less than 1 minute
In ROS2 nodes, whenever we write a C++ or Python program, we have to bring in two operations: "initialisation" and "resource release". For example:
In C++:
#include "rclcpp/rclcpp.hpp"
class MyNode: public rclcpp::Node{
public:
MyNode():Node("hello_node_cpp"){
RCLCPP_INFO(this->get_logger(), "helloworld");
}
};
int main(int argc, char const *argv[])
{
// Initialisation
rclcpp::init(argc, argv);
// Instantiate the custom class
auto node = std::make_shared<MyNode>();
// ......
// Resource release
rclcpp::shutdown();
return 0;
}In Python:
import rclpy
from rclpy.node import Node
class MyNode(Node):
def __init__(self):
super().__init__("hello_node_py")
self.get_logger().info("helloworld!")
def main():
# Initialisation
rclpy.init()
# Create the object
node = MyNode()
# ......
# Resource release
rclpy.shutdown()
if __name__ == '__main__':
main()But once you compile and run it, neither operation shows anything at all. So what are they actually doing?
The answer
- A program built on ROS2 may consist of several steps or stages:
- initialisation -> node object -> log output -> data publishing -> data subscription -> …… -> resource release;
- Data gets passed between the different steps or stages;
- So how do you pass that data?
- Use a Context object. You can treat it as a data container that flows through the different steps, which is what gets the data across
- So initialisation includes the step of creating the Context object, and resource release includes destroying the Context object.
- It's a bit like how the VUE framework, during initialisation, creates all sorts of cache space across the lifecycle in order to achieve reactivity
Important
This article was translated by Hermes Agent; the original text can be found at here. The translation is for reference only. Translation time: 2026-11-05
