netty 处理引用计数异常的srcIndex:0的问题解决

failure: java.lang.IndexOutOfBoundsException: srcIndex: 0

这是因为Netty有引用计数器的原因,自从Netty 4开始,对象的生命周期由它们的引用计数(reference counts)管理,而不是由垃圾收集器(garbage collector)管理了。ByteBuf是最值得注意的,它使用了引用计数来改进分配内存和释放内存的性能。在我们创建ByteBuf对象后,它的引用计数是1,当你释放(release)引用计数对象时,它的引用计数减1,如果引用计数为0,这个引用计数对象会被释放(deallocate),并返回对象池。当尝试访问引用计数为0的引用计数对象会抛出IllegalReferenceCountException异常:

/** 
* Should be called by every method that tries to access the buffers content to check 
* if the buffer was released before. 
*/ 
protected final void ensureAccessible() { 
    if (checkAccessible && refCnt() == 0) { 
        throw new IllegalReferenceCountException(0); 
    } 
}

解决办法

方法1:在合适的地方,补上 ByteBuf.retain(),这个意思是 让netty的引用计数+1
如:
//netty需要用ByteBuf传输
ByteBuf buf = Unpooled.buffer();
// 对所有客户端遍历发送数据
for (Channel client : channelGroup) {
// 处理引用计数异常的srcIndex:0的问题
buf.retain();
sendMessage(client, buf);
}

private void sendMessage(Channel client, ByteBuf bufMessage) {
client.writeAndFlush(bufMessage).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
log.info("回写成功 : {}", client.remoteAddress());
} else {
log.error("回写失败 : {}", client.remoteAddress());
}
}
});
}
posted @ 2022-04-06 10:37  adao  阅读(153)  评论(0编辑  收藏  举报