title: Netty: IllegalReferenceCountException issue in Netty 4.0.14?
body: <p>Hi I am very new to Netty, as I just recently took over the netty customization project from a colleague. Sorry if this has been asked before (I did search already). Our Netty version is <code>4.0.14</code>.</p>
  <p>I keep getting <code>IllegalReferenceCountException</code> on a tiny subset of my calls to <code>channel.writeAndFlush(response);</code></p>
  <p>
  So I added some debug printout in Netty, and it seems that for the same call to writeAndFlush netty may release the (ByteBuf)response.content() multiple times. Is this a known issue that is already fixed after 4.0.14, or am I using it wrong?
  </p>
  <p>
  My debug print out below. There are four times that the ByteBuf's retain/release is called. 
  </p>
  <p>Release <code>(refCnt 1-&gt;0)</code> followed by a Retain <code>(refCnt 0-&gt;1)</code>, then a release <code>(1-&gt;0)</code> and a release <code>(0-&gt;-1)</code>:</p>
  <blockquote>
  <p><strong>1 -&gt; 0</strong> | io.netty.buffer.AbstractReferenceCountedByteBuf.release 142 |
  io.netty.handler.codec.http.DefaultFullHttpResponse.release 79 |
  io.netty.util.ReferenceCountUtil.release 68 |
  io.netty.handler.codec.MessageToMessageEncoder.write 91 |
  io.netty.handler.codec.MessageToMessageCodec.write 116 |
  io.netty.channel.DefaultChannelHandlerContext.invokeWrite 645 |
  io.netty.channel.DefaultChannelHandlerContext.write 699 |
  io.netty.channel.DefaultChannelHandlerContext.write 638 |
  io.netty.handler.timeout.IdleStateHandler.write 265 |
  io.netty.channel.DefaultChannelHandlerContext.invokeWrite 645 |
  io.netty.channel.DefaultChannelHandlerContext.access$2000 29 |
  io.netty.channel.DefaultChannelHandlerContext$WriteTask.run 906 |
  io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks 354 |
  io.netty.channel.nio.NioEventLoop.run 353 |
  io.netty.util.concurrent.SingleThreadEventExecutor$2.run 101 |
  java.lang.Thread.run 745</p>
  <p><strong>0 -&gt; 1</strong>  | io.netty.buffer.AbstractReferenceCountedByteBuf.retain 99 |
  io.netty.buffer.CompositeByteBuf.retain 1570 |
  io.netty.buffer.CompositeByteBuf.retain 40 |
  io.netty.handler.codec.http.HttpObjectEncoder.encodeAndRetain 165 |
  io.netty.handler.codec.http.HttpObjectEncoder.encode 92 |
  io.netty.handler.codec.MessageToMessageEncoder.write 89 |
  io.netty.channel.CombinedChannelDuplexHandler.write 193 |
  io.netty.channel.DefaultChannelHandlerContext.invokeWrite 645 |
  io.netty.channel.DefaultChannelHandlerContext.write 699 |
  io.netty.channel.DefaultChannelHandlerContext.write 638 |
  io.netty.handler.codec.MessageToMessageEncoder.write 115 |
  io.netty.handler.codec.MessageToMessageCodec.write 116 |
  io.netty.channel.DefaultChannelHandlerContext.invokeWrite 645 |
  io.netty.channel.DefaultChannelHandlerContext.write 699 |
  io.netty.channel.DefaultChannelHandlerContext.write 638 |
  io.netty.handler.timeout.IdleStateHandler.write 265 |
  io.netty.channel.DefaultChannelHandlerContext.invokeWrite 645 |
  io.netty.channel.DefaultChannelHandlerContext.access$2000 29 |
  io.netty.channel.DefaultChannelHandlerContext$WriteTask.run 906 |
  io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks 354 |
  io.netty.channel.nio.NioEventLoop.run 353 |
  io.netty.util.concurrent.SingleThreadEventExecutor$2.run 101 |
  java.lang.Thread.run 745</p>
  <p><strong>1 -&gt; 0</strong>  | io.netty.buffer.AbstractReferenceCountedByteBuf.release 142
  | io.netty.handler.codec.http.DefaultFullHttpResponse.release 79 |
  io.netty.util.ReferenceCountUtil.release 68 |
  io.netty.handler.codec.MessageToMessageEncoder.write 91 |
  io.netty.channel.CombinedChannelDuplexHandler.write 193 |
  io.netty.channel.DefaultChannelHandlerContext.invokeWrite 645 |
  io.netty.channel.DefaultChannelHandlerContext.write 699 |
  io.netty.channel.DefaultChannelHandlerContext.write 638 |
  io.netty.handler.codec.MessageToMessageEncoder.write 115 |
  io.netty.handler.codec.MessageToMessageCodec.write 116 |
  io.netty.channel.DefaultChannelHandlerContext.invokeWrite 645 |
  io.netty.channel.DefaultChannelHandlerContext.write 699 |
  io.netty.channel.DefaultChannelHandlerContext.write 638 |
  io.netty.handler.timeout.IdleStateHandler.write 265 |
  io.netty.channel.DefaultChannelHandlerContext.invokeWrite 645 |
  io.netty.channel.DefaultChannelHandlerContext.access$2000 29 |
  io.netty.channel.DefaultChannelHandlerContext$WriteTask.run 906 |
  io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks 354 |
  io.netty.channel.nio.NioEventLoop.run 353 |
  io.netty.util.concurrent.SingleThreadEventExecutor$2.run 101 |
  java.lang.Thread.run 745</p>
  <p><strong>0 -&gt; -1</strong>  | io.netty.buffer.AbstractReferenceCountedByteBuf.release 142
  | io.netty.util.ReferenceCountUtil.release 68 |
  io.netty.channel.ChannelOutboundBuffer.safeRelease 509 |
  io.netty.channel.ChannelOutboundBuffer.remove 294 |
  io.netty.channel.ChannelOutboundBuffer.failFlushed 448 |
  io.netty.channel.AbstractChannel$AbstractUnsafe.flush0 684 |
  io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.flush0 271 |
  io.netty.channel.AbstractChannel$AbstractUnsafe.flush 651 |
  io.netty.channel.DefaultChannelPipeline$HeadHandler.flush 1038 |
  io.netty.channel.DefaultChannelHandlerContext.invokeFlush 675 |
  io.netty.channel.DefaultChannelHandlerContext.flush 656 |
  io.netty.channel.ChannelOutboundHandlerAdapter.flush 115 |
  io.netty.channel.CombinedChannelDuplexHandler.flush 198 |
  io.netty.channel.DefaultChannelHandlerContext.invokeFlush 675 |
  io.netty.channel.DefaultChannelHandlerContext.flush 656 |
  io.netty.channel.ChannelDuplexHandler.flush 117 |
  io.netty.channel.DefaultChannelHandlerContext.invokeFlush 675 |
  io.netty.channel.DefaultChannelHandlerContext.flush 656 |
  io.netty.channel.ChannelDuplexHandler.flush 117 |
  io.netty.channel.DefaultChannelHandlerContext.invokeFlush 675 |
  io.netty.channel.DefaultChannelHandlerContext.access$1600 29 |
  io.netty.channel.DefaultChannelHandlerContext$WriteTask.run 908 |
  io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks 354 |
  io.netty.channel.nio.NioEventLoop.run 353 |
  io.netty.util.concurrent.SingleThreadEventExecutor$2.run 101 |
  java.lang.Thread.run 745</p>
  </blockquote>