From f85af462620399ec96952ed1997ed14eb635bd93 Mon Sep 17 00:00:00 2001 From: Jason Neufeld Date: Wed, 3 Oct 2018 12:09:30 -0700 Subject: [PATCH] Adds __reset method to Struct and Table (#4966) This allow recycling/pooling instances without leaking ByteBuffers, by providing a mechanism to reset instance to newly constructed state. --- java/com/google/flatbuffers/Struct.java | 14 ++++++++++++++ java/com/google/flatbuffers/Table.java | 12 ++++++++++++ 2 files changed, 26 insertions(+) diff --git a/java/com/google/flatbuffers/Struct.java b/java/com/google/flatbuffers/Struct.java index ae3155314..39a8215ad 100644 --- a/java/com/google/flatbuffers/Struct.java +++ b/java/com/google/flatbuffers/Struct.java @@ -28,6 +28,20 @@ public class Struct { protected int bb_pos; /** The underlying ByteBuffer to hold the data of the Struct. */ protected ByteBuffer bb; + + /** + * Resets internal state with a null {@code ByteBuffer} and a zero position. + * + * This method exists primarily to allow recycling Struct instances without risking memory leaks + * due to {@code ByteBuffer} references. The instance will be unusable until it is assigned + * again to a {@code ByteBuffer}. + * + * @param struct the instance to reset to initial state + */ + public void __reset() { + bb = null; + bb_pos = 0; + } } /// @endcond diff --git a/java/com/google/flatbuffers/Table.java b/java/com/google/flatbuffers/Table.java index cbbeda114..489893b66 100644 --- a/java/com/google/flatbuffers/Table.java +++ b/java/com/google/flatbuffers/Table.java @@ -292,6 +292,18 @@ public class Table { } return len_1 - len_2; } + + /** + * Resets the internal state with a null {@code ByteBuffer} and a zero position. + * + * This method exists primarily to allow recycling Table instances without risking memory leaks + * due to {@code ByteBuffer} references. The instance will be unusable until it is assigned + * again to a {@code ByteBuffer}. + */ + public void __reset() { + bb = null; + bb_pos = 0; + } } /// @endcond