Skip to content

Commit

Permalink
This closes rosjava#233. Adds fix for shutting down DefaultNodeMainEx…
Browse files Browse the repository at this point in the history
…ecutor ListenerGroup to prevent leak in android when activities are destroyed. Added ability to remove listener from ListenerGroup to fix android_core issue rosjava#254.
  • Loading branch information
stratomda authored and Julian Cerruti committed Mar 6, 2017
1 parent c860695 commit 5aa7b3e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions rosjava/src/main/java/org/ros/concurrent/EventDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public void loop() throws InterruptedException {
SignalRunnable<T> signalRunnable = events.takeFirst();
signalRunnable.run(listener);
}

public T getListener()
{
return listener;
}
}
20 changes: 20 additions & 0 deletions rosjava/src/main/java/org/ros/concurrent/ListenerGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,25 @@ public Collection<EventDispatcher<T>> addAll(Collection<T> listeners) {
return addAll(listeners, DEFAULT_QUEUE_CAPACITY);
}

/**
* Removes and cancels the {@EventDispatcher} specified by the listener
* from the {@link ListenerGroup}.
* @param listener the listener to remove
* @return flag indicating successful removal
*/
public boolean remove(T listener)
{
for (EventDispatcher<T> eventDispatcher : eventDispatchers) {
if(listener.equals(eventDispatcher.getListener()))
{
eventDispatcher.cancel();
eventDispatchers.remove(eventDispatcher);
return true;
}
}
return false;
}

/**
* @return the number of listeners in the group
*/
Expand Down Expand Up @@ -151,5 +170,6 @@ public void shutdown() {
for (EventDispatcher<T> eventDispatcher : eventDispatchers) {
eventDispatcher.cancel();
}
eventDispatchers.clear();
}
}
5 changes: 5 additions & 0 deletions rosjava/src/main/java/org/ros/internal/node/DefaultNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,11 @@ public void run(NodeListener listener) {
});
}

@Override
public void removeListeners() {
nodeListeners.shutdown();
}

/**
* SignalRunnable all {@link NodeListener}s that the {@link Node} has started.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ private void registerNode(ConnectedNode connectedNode) {
* the {@link Node} to unregister
*/
private void unregisterNode(Node node) {
node.removeListeners();
connectedNodes.get(node.getName()).remove(node);
nodeMains.remove(node);
}
Expand Down
6 changes: 6 additions & 0 deletions rosjava/src/main/java/org/ros/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,10 @@ public interface Node {
* Shut the node down.
*/
void shutdown();

/**
* Stops and Clears node listeners.
*/

void removeListeners();
}

0 comments on commit 5aa7b3e

Please sign in to comment.