io.netty.IllegalReferenceCountException: refCnt: 0 exception.

1,119 views
Skip to first unread message

khayam...@gmail.com

unread,
Mar 19, 2018, 2:35:01 PM3/19/18
to Netty discussions
Hi. I am receiving data over a netty channel and than after striping 4 bytes I am writing it to another netty channel.  Following function is trying to achieve this.

@Override
public void orderedPacket(ByteBuf packet) {
// log.info(packet.toString());
byte[] bytes = new byte[packet.capacity() - 4 ];
packet.getBytes(4, bytes);
ChannelFuture cf = hostChannel.writeAndFlush(bytes);

// if (!cf.isSuccess()) log.error("write back to host not successful {}", cf.cause());

}

However it throws following exception. 


11:28:21.091 [nioEventLoopGroup-4-4] WARN  i.n.channel.DefaultChannelPipeline - An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
io.netty.util.IllegalReferenceCountException: refCnt: 0
        at io.netty.buffer.AbstractByteBuf.ensureAccessible(AbstractByteBuf.java:1415) ~[sosagent.jar:1.0-SNAPSHOT]
        at io.netty.buffer.AbstractByteBuf.checkIndex(AbstractByteBuf.java:1354) ~[sosagent.jar:1.0-SNAPSHOT]
        at io.netty.buffer.UnsafeByteBufUtil.getBytes(UnsafeByteBufUtil.java:481) ~[sosagent.jar:1.0-SNAPSHOT]
        at io.netty.buffer.PooledUnsafeDirectByteBuf.getBytes(PooledUnsafeDirectByteBuf.java:131) ~[sosagent.jar:1.0-SNAPSHOT]
        at io.netty.buffer.PooledSlicedByteBuf.getBytes(PooledSlicedByteBuf.java:233) ~[sosagent.jar:1.0-SNAPSHOT]
        at io.netty.buffer.AbstractByteBuf.getBytes(AbstractByteBuf.java:474) ~[sosagent.jar:1.0-SNAPSHOT]
        at edu.clemson.openflow.sos.agent.AgentClient.orderedPacket(AgentClient.java:81) ~[sosagent.jar:1.0-SNAPSHOT]
        at edu.clemson.openflow.sos.buf.OrderedPacketInitiator.orderedPacket(OrderedPacketInitiator.java:17) ~[sosagent.jar:1.0-SNAPSHOT]
        at edu.clemson.openflow.sos.buf.Buffer.sendData(Buffer.java:84) ~[sosagent.jar:1.0-SNAPSHOT]



The bold line in stack-trace corresponds to  packet.getBytes(4, bytes); in my method.

Any idea what might be problem here.



Norman Maurer

unread,
Mar 19, 2018, 3:36:12 PM3/19/18
to ne...@googlegroups.com
Seems like you not correctly retain the ByteBuf before hand it over to your method and so it is already released. You need to ensure you call buffer.retain() before hand it over (as it seems your method runs in another thread as the ChannelHandler itself) and then buffer.release() after you are done with it.

Bye
Norman



--
You received this message because you are subscribed to the Google Groups "Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netty+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netty/e46725a9-eea6-45b5-99bc-140a3b907a11%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages