This is an automated email from the ASF dual-hosted git repository. jfeinauer pushed a commit to branch fix-bytebuff-leaks in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit 3dadbce187a488e27e169eaf280443d7061ce9e9 Author: julian <[email protected]> AuthorDate: Thu Aug 8 12:38:03 2019 +0200 Iniital Refactoring. --- .../apache/plc4x/java/base/messages/PlcRawMessage.java | 16 ++++++++++++++++ .../plc4x/java/isoontcp/protocol/IsoOnTcpProtocol.java | 6 ++++++ 2 files changed, 22 insertions(+) diff --git a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/messages/PlcRawMessage.java b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/messages/PlcRawMessage.java index b6205d9..bde8e93 100644 --- a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/messages/PlcRawMessage.java +++ b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/messages/PlcRawMessage.java @@ -32,9 +32,13 @@ public class PlcRawMessage implements PlcProtocolMessage { public PlcRawMessage(ByteBuf userData, PlcProtocolMessage parent) { this.userData = userData; this.parent = parent; + // Retain ByteBuf for Rec Counting + this.userData.retain(); } public ByteBuf getUserData() { + // Increment Ref Count because the client has to free the object also + this.userData.retain(); return userData; } @@ -43,4 +47,16 @@ public class PlcRawMessage implements PlcProtocolMessage { return parent; } + /** + * This method calls the release method of the underlying Netty {@link ByteBuf} and + * has to be called when the Message Object is no longer used. + * + * Otherwise this could lead to a LEAK. + * + * Important, this should not be called TWICE but exactly ONCE. + */ + public void release() { + this.userData.release(); + } + } diff --git a/plc4j/protocols/iso-on-tcp/src/main/java/org/apache/plc4x/java/isoontcp/protocol/IsoOnTcpProtocol.java b/plc4j/protocols/iso-on-tcp/src/main/java/org/apache/plc4x/java/isoontcp/protocol/IsoOnTcpProtocol.java index afd6146..ee375ba 100644 --- a/plc4j/protocols/iso-on-tcp/src/main/java/org/apache/plc4x/java/isoontcp/protocol/IsoOnTcpProtocol.java +++ b/plc4j/protocols/iso-on-tcp/src/main/java/org/apache/plc4x/java/isoontcp/protocol/IsoOnTcpProtocol.java @@ -61,6 +61,10 @@ public class IsoOnTcpProtocol extends PlcByteToMessageCodec<IsoOnTcpMessage> { // Output the payload. out.writeBytes(userData); + + // Release the ByteBuf and the IsoOnTcpMessage + userData.release(); + in.release(); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -97,8 +101,10 @@ public class IsoOnTcpProtocol extends PlcByteToMessageCodec<IsoOnTcpMessage> { // Skip the 4 bytes we peeked into manually. in.skipBytes(4); // Simply place the current buffer to the output ... the next handler will continue. + // TODO we have to free the ByteBuff here (but also propagate that through the object) ByteBuf payload = in.readBytes(packetLength - 4); out.add(new IsoOnTcpMessage(payload)); + payload.release(); /*} else { chunkedResponse = Unpooled.buffer(packetLength); chunkedResponse.writeBytes(in, packetLength);*/
