Skip to content

Commit 2372ea0

Browse files
committed
Fixed image queue leak
1 parent 4544f6e commit 2372ea0

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

ihmc-high-level-behaviors/src/main/java/us/ihmc/perception/StandAloneRealsenseProcess.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public StandAloneRealsenseProcess(ROS2Node ros2Node,
8585
d455PublishThread.addTopic(PerceptionAPI.D455_DEPTH_IMAGE, RealSenseImageSensor.DEPTH_IMAGE_KEY);
8686
loopOnDemand(d455PublishThread, realsensePublishDemandNode);
8787

88-
BlockingQueue<RawImage> rawImageCollection = new LinkedBlockingQueue<>();
88+
BlockingQueue<RawImage> rawImageCollection = new LinkedBlockingQueue<>(10);
8989
d455Sensor.registerImageCollector(rawImageCollection, RealSenseImageSensor.DEPTH_IMAGE_KEY);
9090
rapidHeightMapThread = new RapidHeightMapThread(ros2Helper.getROS2Node(),
9191
syncedRobot,

ihmc-perception/src/main/java/us/ihmc/sensors/ImageSensor.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import us.ihmc.perception.RawImage;
88

99
import java.util.ArrayList;
10-
import java.util.Collection;
1110
import java.util.HashMap;
1211
import java.util.List;
1312
import java.util.Map;
13+
import java.util.concurrent.BlockingQueue;
1414

1515
public abstract class ImageSensor implements AutoCloseable
1616
{
@@ -22,7 +22,7 @@ public abstract class ImageSensor implements AutoCloseable
2222

2323
private final RepeatingTaskThread grabThread;
2424
private final Object grabNotification = new Object();
25-
private final Map<Integer, List<Collection<RawImage>>> imageCollectors = new HashMap<>();
25+
private final Map<Integer, List<BlockingQueue<RawImage>>> imageQueues = new HashMap<>();
2626

2727
public ImageSensor(String sensorName)
2828
{
@@ -105,24 +105,25 @@ public ReferenceFrame getSensorFrame()
105105
public abstract ReferenceFrame[] getImageFrames();
106106

107107
/**
108-
* Register an image collector for images of a particular key.
108+
* Register an image queue for images of a particular key.
109109
* <p>
110-
* Every image grabbed by the sensor will be added to the passed in collection.
110+
* Every image grabbed by the sensor will be added to the passed in queue.
111111
* The code accessing the collection must call {@link RawImage#release()} on each image,
112112
* once it's done using the image.
113113
* <p>
114-
* Although any class that implements {@link Collection} can be passed in, the implementation of
115-
* the {@link Collection#add(Object)} method should not take a significant amount of time.
114+
* If the queue becomes full (i.e. when {@code BlockingQueue#remainingCapacity() == 0})
115+
* the oldest image will be removed so the new image can be added.
116+
* Ensure a reasonable queue capacity is set to prevent memory leaks.
116117
*
117-
* @param imageCollector Collection into which the images will be added.
118+
* @param imageQueue Blocking queue into which the images will be added.
118119
* @param imageKey The key for images to be collected.
119120
*/
120-
public void registerImageCollector(Collection<RawImage> imageCollector, int imageKey)
121+
public void registerImageCollector(BlockingQueue<RawImage> imageQueue, int imageKey)
121122
{
122-
if (imageCollectors.containsKey(imageKey))
123-
imageCollectors.get(imageKey).add(imageCollector);
123+
if (imageQueues.containsKey(imageKey))
124+
imageQueues.get(imageKey).add(imageQueue);
124125
else
125-
imageCollectors.put(imageKey, new ArrayList<>(List.of(imageCollector)));
126+
imageQueues.put(imageKey, new ArrayList<>(List.of(imageQueue)));
126127
}
127128

128129
public String getSensorName()
@@ -189,8 +190,16 @@ private void grabAndNotify()
189190

190191
for (int imageKey : getImageKeys())
191192
{
192-
if (imageCollectors.containsKey(imageKey))
193-
imageCollectors.get(imageKey).forEach(collector -> collector.add(getImage(imageKey)));
193+
if (imageQueues.containsKey(imageKey))
194+
{
195+
imageQueues.get(imageKey).forEach(queue ->
196+
{
197+
if (queue.remainingCapacity() == 0)
198+
queue.remove().release();
199+
200+
queue.add(getImage(imageKey));
201+
});
202+
}
194203
}
195204
}
196205
}

0 commit comments

Comments
 (0)