Fix buffer leaks

This commit is contained in:
Norman Maurer 2013-07-14 22:50:53 +02:00
parent 9dfad35423
commit ecb215c12f
7 changed files with 27 additions and 9 deletions

View File

@ -211,7 +211,9 @@ public abstract class HttpContentDecoder extends MessageToMessageDecoder<HttpObj
private void cleanup() { private void cleanup() {
if (decoder != null) { if (decoder != null) {
// Clean-up the previous decoder if not cleaned up correctly. // Clean-up the previous decoder if not cleaned up correctly.
finishDecode(Unpooled.buffer()); ByteBuf buf = Unpooled.buffer();
finishDecode(buf);
buf.release();
} }
} }
@ -236,6 +238,7 @@ public abstract class HttpContentDecoder extends MessageToMessageDecoder<HttpObj
break; break;
} }
out.writeBytes(buf); out.writeBytes(buf);
buf.release();
} }
} }
} }

View File

@ -708,8 +708,10 @@ public class HttpPostRequestEncoder implements ChunkedInput<HttpContent> {
HttpContent chunk = nextChunk(); HttpContent chunk = nextChunk();
if (request instanceof FullHttpRequest) { if (request instanceof FullHttpRequest) {
FullHttpRequest fullRequest = (FullHttpRequest) request; FullHttpRequest fullRequest = (FullHttpRequest) request;
if (!fullRequest.content().equals(chunk.content())) { ByteBuf chunkContent = chunk.content();
fullRequest.content().clear().writeBytes(chunk.content()); if (fullRequest.content() != chunkContent) {
fullRequest.content().clear().writeBytes(chunkContent);
chunkContent.release();
} }
return fullRequest; return fullRequest;
} else { } else {

View File

@ -76,6 +76,10 @@ public class StringEncoder extends MessageToByteEncoder<CharSequence> {
} }
ByteBuf encoded = Unpooled.copiedBuffer(msg, charset); ByteBuf encoded = Unpooled.copiedBuffer(msg, charset);
try {
out.writeBytes(encoded); out.writeBytes(encoded);
} finally {
encoded.release();
}
} }
} }

View File

@ -15,6 +15,7 @@
*/ */
package io.netty.example.http.file; package io.netty.example.http.file;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
@ -272,8 +273,9 @@ public class HttpStaticFileServerHandler extends SimpleChannelInboundHandler<Ful
} }
buf.append("</ul></body></html>\r\n"); buf.append("</ul></body></html>\r\n");
ByteBuf buffer = Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8);
response.content().writeBytes(Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8)); response.content().writeBytes(buffer);
buffer.release();
// Close the connection as soon as the error message is sent. // Close the connection as soon as the error message is sent.
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);

View File

@ -15,6 +15,7 @@
*/ */
package io.netty.example.http.websocketx.autobahn; package io.netty.example.http.websocketx.autobahn;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
@ -122,7 +123,9 @@ public class AutobahnServerHandler extends ChannelInboundHandlerAdapter {
ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
// Generate an error page if response getStatus code is not OK (200). // Generate an error page if response getStatus code is not OK (200).
if (res.getStatus().code() != 200) { if (res.getStatus().code() != 200) {
res.content().writeBytes(Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8)); ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
res.content().writeBytes(buf);
buf.release();
setContentLength(res, res.content().readableBytes()); setContentLength(res, res.content().readableBytes());
} }

View File

@ -135,7 +135,9 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object>
ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
// Generate an error page if response getStatus code is not OK (200). // Generate an error page if response getStatus code is not OK (200).
if (res.getStatus().code() != 200) { if (res.getStatus().code() != 200) {
res.content().writeBytes(Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8)); ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
res.content().writeBytes(buf);
buf.release();
setContentLength(res, res.content().readableBytes()); setContentLength(res, res.content().readableBytes());
} }

View File

@ -132,7 +132,9 @@ public class WebSocketSslServerHandler extends SimpleChannelInboundHandler<Objec
ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
// Generate an error page if response getStatus code is not OK (200). // Generate an error page if response getStatus code is not OK (200).
if (res.getStatus().code() != 200) { if (res.getStatus().code() != 200) {
res.content().writeBytes(Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8)); ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
res.content().writeBytes(buf);
buf.release();
setContentLength(res, res.content().readableBytes()); setContentLength(res, res.content().readableBytes());
} }