添加URL
分享

在netty4中,对象的生命周期由引用计数器控制,ByteBuf就是如此,每个对象的初始化引用计数加1,调用一次release方法,引用计数器会减1,当尝试访问计数器为0时,对象时,会抛出 IllegalReferenceCountException ,正如 ensureAccessible 的实现,更加详细的解释可以参考 官方文档

原因分析:日志显示refCnt=0了,表明使用的这个ByteBuf已经被回收了,代码中调用ctx.writeAndFlush(buff)会使此次ByteBuf回收也即将refCnt置为0

个人理解:ack消息代码里;

buff.release(); netty提供的一个释放ByteBuf内存的方法(ReferenceCountUtil.release(msg);), 如果不采用这个,直接调用ByteBuf.release方法也可以,但是因为上面调用了writeAndFlush方法, 已经将ByteBuf的refCnt置为0了,这个里面调用的时候又会在设置一次,但是发现已经为0了, 所以就抛出的该异常。

报错信息:

io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1 at io.netty.buffer.AbstractReferenceCountedByteBuf.release(AbstractReferenceCountedByteBuf.java:101) at com.alex.data.netty.utils.NettyUtil.sendMessage(NettyUtil.java:35) at com.alex.data.netty.service.ServiceMessage.handlerVehLoginData(ServiceMessage.java:124) at com.alex.data.netty.service.ServiceMessage.analysisVehDataByMessage(ServiceMessage.java:69) at com.alex.data.netty.server.VehMessageHandler.channelRead(VehMessageHandler.java:72) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304) at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:266) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304) at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:276) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:263) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354) at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112) at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137) at java.lang.Thread.run(Thread.java:745)

ack消息的方式如下

* 发送消息 * @param msg * @param channel public static void sendMessage(byte[] msg, Channel channel) { if(channel == null) { logger.error("channel == null,can't ack msg|"+ CodecUtil.byte2HexStr(msg)); return; ByteBuf buff = channel.alloc().buffer(); buff.writeBytes(msg); channel.writeAndFlush(buff); //TODO buff.release();//此处会引起 对象的生命周期由引用计数器控制异常分析 IllegalReferenceCountException refCnt: 0, decrement: 1 文章最后发布于: 2018-09-30