Fix false-positive leaks

- All derived buffers and swapped buffers of a leak-aware buffer must be wrapped again with the leak-aware buffer
This commit is contained in:
Trustin Lee 2013-12-06 21:32:33 +09:00
parent 0f3451c227
commit 187a5976cc
2 changed files with 92 additions and 43 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}