Skip to content

Commit

Permalink
Fix more potential memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
ds58 committed Feb 13, 2025
1 parent a5e4c4c commit 79dcb53
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package us.ihmc.pubsub.impl.fastRTPS;

import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacpp.Pointer;
import us.ihmc.pubsub.TopicDataType;
import us.ihmc.pubsub.attributes.PublisherAttributes;
import us.ihmc.pubsub.common.Guid;
Expand All @@ -32,6 +35,11 @@

public class FastRTPSPublisher implements Publisher
{
static
{
Loader.load(Pointer.class);
}

private final Object destructorLock = new Object();

private NativePublisherImpl impl;
Expand All @@ -41,7 +49,8 @@ public class FastRTPSPublisher implements Publisher
private final SerializedPayload payload;
private final Guid guid = new Guid();

private final ByteBuffer keyBuffer = ByteBuffer.allocateDirect(16);
private final Pointer keyBufferPointer = new BytePointer(16);
private final ByteBuffer keyBuffer = keyBufferPointer.asByteBuffer();
private final NativePublisherListenerImpl nativeListenerImpl = new NativePublisherListenerImpl();

private boolean isRemoved = false;
Expand Down Expand Up @@ -190,6 +199,8 @@ void delete()
{
synchronized(destructorLock)
{
keyBufferPointer.close();
payload.close();
impl.delete();
nativeListenerImpl.delete();
impl = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package us.ihmc.pubsub.impl.fastRTPS;

import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacpp.Pointer;
import us.ihmc.idl.CDR;
import us.ihmc.pubsub.TopicDataType;
import us.ihmc.pubsub.attributes.SubscriberAttributes;
Expand All @@ -38,6 +41,11 @@

public class FastRTPSSubscriber<T> implements Subscriber<T>
{
static
{
Loader.load(Pointer.class);
}

private final Object destructorLock = new Object();

private NativeSubscriberImpl impl;
Expand All @@ -48,11 +56,11 @@ public class FastRTPSSubscriber<T> implements Subscriber<T>
private final SerializedPayload payload;
private final Guid guid = new Guid();
private final MatchingInfo matchingInfo = new MatchingInfo();

private final ByteBuffer keyBuffer = ByteBuffer.allocateDirect(16);

private final SampleInfoMarshaller sampleInfoMarshaller = new SampleInfoMarshaller();

private final Pointer keyBufferPointer = new BytePointer(16);
private final ByteBuffer keyBuffer = keyBufferPointer.asByteBuffer();
private final NativeSubscriberListenerImpl nativeListenerImpl = new NativeSubscriberListenerImpl();

private boolean hasMatched = false;
Expand Down Expand Up @@ -326,6 +334,7 @@ void delete()
{
synchronized(destructorLock)
{
keyBufferPointer.close();
payload.close();
impl.delete();
nativeListenerImpl.delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@
*/
package us.ihmc.pubsub.types;

import java.io.IOException;
import java.nio.ByteBuffer;

import org.apache.commons.lang3.NotImplementedException;

import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacpp.Pointer;
import us.ihmc.idl.CDR;
import us.ihmc.idl.InterchangeSerializer;
import us.ihmc.pubsub.TopicDataType;
import us.ihmc.pubsub.common.SerializedPayload;

import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
* A generic data type to send over raw byte buffers.
*
Expand All @@ -40,12 +43,19 @@
* @author Jesper Smith
*
*/
public class ByteBufferPubSubType implements TopicDataType<ByteBuffer>
public class ByteBufferPubSubType implements TopicDataType<ByteBuffer>, AutoCloseable
{
static
{
Loader.load(Pointer.class);
}

private final String name;
private final int maxSize;
private final String userName;
private final int userMaxSize;
@Nullable
private Pointer dataPointer;

private static int align(int size)
{
Expand Down Expand Up @@ -121,7 +131,8 @@ public String getName()
@Override
public ByteBuffer createData()
{
return ByteBuffer.allocateDirect(maxSize);
dataPointer = new BytePointer(maxSize);
return dataPointer.asByteBuffer();
}

@Override
Expand Down Expand Up @@ -159,4 +170,11 @@ public void deserialize(InterchangeSerializer serializer, ByteBuffer data)
{
throw new NotImplementedException("Interchange serializer is not implemented for bytebuffer pub/sub type");
}

@Override
public void close()
{
if (dataPointer != null)
dataPointer.close();
}
}

0 comments on commit 79dcb53

Please sign in to comment.