Ensure uncompressed ByteBuf is released when an exception happens during decoding.

Motivation:

We need to ensure the uncompressed ByteBuf is released if an exception happens while calling decode(...). If we miss to do so we leak buffers.

Modifications:

Correctly release buffer on exception.

Result:

No more memory leak.
This commit is contained in:
Norman Maurer 2016-07-20 06:35:05 +02:00
parent 94d7557dea
commit 7db3e01498

View File

@ -166,20 +166,27 @@ public class SnappyFrameDecoder extends ByteToMessageDecoder {
in.skipBytes(4); in.skipBytes(4);
int checksum = in.readIntLE(); int checksum = in.readIntLE();
ByteBuf uncompressed = ctx.alloc().buffer(0); ByteBuf uncompressed = ctx.alloc().buffer();
if (validateChecksums) { try {
int oldWriterIndex = in.writerIndex(); if (validateChecksums) {
try { int oldWriterIndex = in.writerIndex();
in.writerIndex(in.readerIndex() + chunkLength - 4); try {
snappy.decode(in, uncompressed); in.writerIndex(in.readerIndex() + chunkLength - 4);
} finally { snappy.decode(in, uncompressed);
in.writerIndex(oldWriterIndex); } finally {
in.writerIndex(oldWriterIndex);
}
validateChecksum(checksum, uncompressed, 0, uncompressed.writerIndex());
} else {
snappy.decode(in.readSlice(chunkLength - 4), uncompressed);
}
out.add(uncompressed);
uncompressed = null;
} finally {
if (uncompressed != null) {
uncompressed.release();
} }
validateChecksum(checksum, uncompressed, 0, uncompressed.writerIndex());
} else {
snappy.decode(in.readSlice(chunkLength - 4), uncompressed);
} }
out.add(uncompressed);
snappy.reset(); snappy.reset();
break; break;
} }