Skip to content

Commit 6ac175f

Browse files
committed
Fix bugs in examples and electron demos (#1414)
This PR fixes small but user-facing issues in the JavaScript examples and Electron demos, improving correctness of sample code and shutdown/cleanup behavior. **Changes:** **example/topics/publisher/publisher-qos-example.js** - Fix typo `qos.hitory` → `qos.history`; the misspelled property silently created a non-existent field, so the history policy was never actually configured **example/actions/action_client/action-client-example.js** - Fix undefined variable `status` → `goalHandle.status` in the goal failure log message; the original code would throw a `ReferenceError` when a goal failed **electron_demo/manipulator/main.js** - Fix resource leak: store the publishing `setInterval` return value in `publishingInterval` and clear it on app shutdown; previously the interval ID was discarded, making cleanup impossible **electron_demo/turtle_tf2/main.js** - Fix incorrect `rclnodejs.shutdown()` usage: call it once globally instead of once per node in a `forEach` loop; `shutdown()` tears down the entire ROS2 context, so calling it multiple times risks context corruption Fix: #1413
1 parent 80b0653 commit 6ac175f

4 files changed

Lines changed: 20 additions & 19 deletions

File tree

electron_demo/manipulator/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ let jointStatePublisher;
2424
let jointStateSubscriber;
2525
let isAnimating = false;
2626
let animationInterval;
27+
let publishingInterval;
2728

2829
// Current joint positions (in radians)
2930
let currentJointPositions = {
@@ -79,6 +80,9 @@ app.whenReady().then(async () => {
7980
app.on('window-all-closed', function () {
8081
if (process.platform !== 'darwin') {
8182
// Clean up ROS2 resources
83+
if (publishingInterval) {
84+
clearInterval(publishingInterval);
85+
}
8286
if (animationInterval) {
8387
clearInterval(animationInterval);
8488
}
@@ -131,7 +135,7 @@ async function initializeROS2() {
131135

132136
function startJointStatePublishing() {
133137
// Publish joint states at 10 Hz
134-
setInterval(() => {
138+
publishingInterval = setInterval(() => {
135139
publishJointStates();
136140
}, 100); // 100ms = 10 Hz
137141
}

electron_demo/turtle_tf2/main.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -759,24 +759,19 @@ app.whenReady().then(async () => {
759759

760760
app.on('window-all-closed', function () {
761761
if (process.platform !== 'darwin') {
762-
// Clean up ROS2 nodes
763-
Object.values(turtleTf2Nodes).forEach((node) => {
764-
try {
765-
rclnodejs.shutdown();
766-
} catch (error) {
767-
console.error('Error shutting down node:', error);
768-
}
769-
});
762+
try {
763+
rclnodejs.shutdown();
764+
} catch (error) {
765+
console.error('Error shutting down ROS2:', error);
766+
}
770767
app.quit();
771768
}
772769
});
773770

774771
app.on('before-quit', () => {
775-
Object.values(turtleTf2Nodes).forEach((node) => {
776-
try {
777-
rclnodejs.shutdown();
778-
} catch (error) {
779-
console.error('Error shutting down node:', error);
780-
}
781-
});
772+
try {
773+
rclnodejs.shutdown();
774+
} catch (error) {
775+
console.error('Error shutting down ROS2:', error);
776+
}
782777
});

example/actions/action_client/action-client-example.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ class FibonacciActionClient {
5353
if (goalHandle.isSucceeded()) {
5454
this._node
5555
.getLogger()
56-
.info(`Goal suceeded with result: ${result.sequence}`);
56+
.info(`Goal succeeded with result: ${result.sequence}`);
5757
} else {
58-
this._node.getLogger().info(`Goal failed with status: ${status}`);
58+
this._node
59+
.getLogger()
60+
.info(`Goal failed with status: ${goalHandle.status}`);
5961
}
6062

6163
rclnodejs.shutdown();

example/topics/publisher/publisher-qos-example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ rclnodejs.init().then(() => {
2121
const node = rclnodejs.createNode('publisher_qos_example_node');
2222

2323
let qos = new QoS();
24-
qos.hitory = QoS.HistoryPolicy.RMW_QOS_POLICY_HISTORY_SYSTEM_DEFAULT;
24+
qos.history = QoS.HistoryPolicy.RMW_QOS_POLICY_HISTORY_SYSTEM_DEFAULT;
2525
qos.reliability =
2626
QoS.ReliabilityPolicy.RMW_QOS_POLICY_RELIABILITY_SYSTEM_DEFAULT;
2727
qos.durability =

0 commit comments

Comments
 (0)