Skip to content

Commit 0b48353

Browse files
authored
clear node/context handle on RclJava.cleanup() (#130)
* clear node/context handle on RclJava.cleanup() * clear publishers, subscriptions, clients, services and timers on Node.dispose()
1 parent 720cbed commit 0b48353

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

rcljava/src/main/java/org/ros2/rcljava/RCLJava.java

+3-20
Original file line numberDiff line numberDiff line change
@@ -80,31 +80,14 @@ private RCLJava() {}
8080

8181
private static void cleanup() {
8282
for (Node node : nodes) {
83-
for (Subscription subscription : node.getSubscriptions()) {
84-
subscription.dispose();
85-
}
86-
87-
for (Publisher publisher : node.getPublishers()) {
88-
publisher.dispose();
89-
}
90-
91-
for (Timer timer : node.getTimers()) {
92-
timer.dispose();
93-
}
94-
95-
for (Service service : node.getServices()) {
96-
service.dispose();
97-
}
98-
99-
for (Client client : node.getClients()) {
100-
client.dispose();
101-
}
102-
10383
node.dispose();
10484
}
85+
nodes.clear();
86+
10587
for (Context context : contexts) {
10688
context.dispose();
10789
}
90+
contexts.clear();
10891
}
10992

11093
static {

rcljava/src/main/java/org/ros2/rcljava/node/NodeImpl.java

+17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.ros2.rcljava.consumers.TriConsumer;
2525
import org.ros2.rcljava.contexts.Context;
2626
import org.ros2.rcljava.qos.QoSProfile;
27+
import org.ros2.rcljava.interfaces.Disposable;
2728
import org.ros2.rcljava.interfaces.MessageDefinition;
2829
import org.ros2.rcljava.interfaces.ServiceDefinition;
2930
import org.ros2.rcljava.parameters.ParameterType;
@@ -343,10 +344,26 @@ public final Collection<Client> getClients() {
343344
*/
344345
private static native void nativeDispose(long handle);
345346

347+
private <T extends Disposable> void cleanupDisposables(Collection<T> disposables) {
348+
for (Disposable disposable : disposables) {
349+
disposable.dispose();
350+
}
351+
disposables.clear();
352+
}
353+
354+
private void cleanup() {
355+
cleanupDisposables(subscriptions);
356+
cleanupDisposables(publishers);
357+
cleanupDisposables(timers);
358+
cleanupDisposables(services);
359+
cleanupDisposables(clients);
360+
}
361+
346362
/**
347363
* {@inheritDoc}
348364
*/
349365
public final void dispose() {
366+
cleanup();
350367
nativeDispose(this.handle);
351368
this.handle = 0;
352369
}

0 commit comments

Comments
 (0)