From 187a5976cc37e82a6e714f382c8398a11bf48720 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Fri, 6 Dec 2013 21:32:33 +0900 Subject: [PATCH] Fix false-positive leaks - All derived buffers and swapped buffers of a leak-aware buffer must be wrapped again with the leak-aware buffer --- .../buffer/AdvancedLeakAwareByteBuf.java | 103 ++++++++++-------- .../netty/buffer/SimpleLeakAwareByteBuf.java | 32 ++++++ 2 files changed, 92 insertions(+), 43 deletions(-) diff --git a/buffer/src/main/java/io/netty/buffer/AdvancedLeakAwareByteBuf.java b/buffer/src/main/java/io/netty/buffer/AdvancedLeakAwareByteBuf.java index 0619dc9ce7..443ee5bb59 100644 --- a/buffer/src/main/java/io/netty/buffer/AdvancedLeakAwareByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/AdvancedLeakAwareByteBuf.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; +import java.nio.ByteOrder; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; import java.nio.charset.Charset; @@ -35,6 +36,62 @@ final class AdvancedLeakAwareByteBuf extends WrappedByteBuf { this.leak = leak; } + @Override + public boolean release() { + boolean deallocated = super.release(); + if (deallocated) { + leak.close(); + } else { + leak.record(); + } + return deallocated; + } + + @Override + public boolean release(int decrement) { + boolean deallocated = super.release(decrement); + if (deallocated) { + leak.close(); + } else { + leak.record(); + } + return deallocated; + } + + @Override + public ByteBuf order(ByteOrder endianness) { + leak.record(); + if (order() == endianness) { + return this; + } else { + return new AdvancedLeakAwareByteBuf(super.order(endianness), leak); + } + } + + @Override + public ByteBuf slice() { + leak.record(); + return new AdvancedLeakAwareByteBuf(super.slice(), leak); + } + + @Override + public ByteBuf slice(int index, int length) { + leak.record(); + return new AdvancedLeakAwareByteBuf(super.slice(index, length), leak); + } + + @Override + public ByteBuf duplicate() { + leak.record(); + return new AdvancedLeakAwareByteBuf(super.duplicate(), leak); + } + + @Override + public ByteBuf readSlice(int length) { + leak.record(); + return new AdvancedLeakAwareByteBuf(super.readSlice(length), leak); + } + @Override public ByteBuf discardReadBytes() { leak.record(); @@ -377,12 +434,6 @@ final class AdvancedLeakAwareByteBuf extends WrappedByteBuf { return super.readBytes(length); } - @Override - public ByteBuf readSlice(int length) { - leak.record(); - return super.readSlice(length); - } - @Override public ByteBuf readBytes(ByteBuf dst) { leak.record(); @@ -605,24 +656,6 @@ final class AdvancedLeakAwareByteBuf extends WrappedByteBuf { return super.copy(index, length); } - @Override - public ByteBuf slice() { - leak.record(); - return super.slice(); - } - - @Override - public ByteBuf slice(int index, int length) { - leak.record(); - return super.slice(index, length); - } - - @Override - public ByteBuf duplicate() { - leak.record(); - return super.duplicate(); - } - @Override public int nioBufferCount() { leak.record(); @@ -684,24 +717,8 @@ final class AdvancedLeakAwareByteBuf extends WrappedByteBuf { } @Override - public boolean release() { - boolean deallocated = super.release(); - if (deallocated) { - leak.close(); - } else { - leak.record(); - } - return deallocated; - } - - @Override - public boolean release(int decrement) { - boolean deallocated = super.release(decrement); - if (deallocated) { - leak.close(); - } else { - leak.record(); - } - return deallocated; + public ByteBuf capacity(int newCapacity) { + leak.record(); + return super.capacity(newCapacity); } } diff --git a/buffer/src/main/java/io/netty/buffer/SimpleLeakAwareByteBuf.java b/buffer/src/main/java/io/netty/buffer/SimpleLeakAwareByteBuf.java index e2061a8fd8..aa61a5c790 100644 --- a/buffer/src/main/java/io/netty/buffer/SimpleLeakAwareByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/SimpleLeakAwareByteBuf.java @@ -18,6 +18,8 @@ package io.netty.buffer; import io.netty.util.ResourceLeak; +import java.nio.ByteOrder; + final class SimpleLeakAwareByteBuf extends WrappedByteBuf { private final ResourceLeak leak; @@ -44,4 +46,34 @@ final class SimpleLeakAwareByteBuf extends WrappedByteBuf { } return deallocated; } + + @Override + public ByteBuf order(ByteOrder endianness) { + leak.record(); + if (order() == endianness) { + return this; + } else { + return new SimpleLeakAwareByteBuf(super.order(endianness), leak); + } + } + + @Override + public ByteBuf slice() { + return new SimpleLeakAwareByteBuf(super.slice(), leak); + } + + @Override + public ByteBuf slice(int index, int length) { + return new SimpleLeakAwareByteBuf(super.slice(index, length), leak); + } + + @Override + public ByteBuf duplicate() { + return new SimpleLeakAwareByteBuf(super.duplicate(), leak); + } + + @Override + public ByteBuf readSlice(int length) { + return new SimpleLeakAwareByteBuf(super.readSlice(length), leak); + } }