netty4.0 read0方法 wirteflush之后,就捕获异常了。

netty4 io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1
网上也看了,是relase了,造成实例被收回。这个要怎么处理?前提这是tcp长连接,怎么也会被收回???

问题已经解决。问题原因为extends了 simple*类而simple又继承了channelInbond类,在fiannly中会对当前msg实例做-1处理,如果下标越界了,就报异常了。处理方式有两种:1,手动+1 2,继承channelInbond*类

protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {

ReferenceCountUtil.retain(msg, 1);//SimpleChannelInboundHandler extends channelInbound*时候会在fiannly中自动将值减1

    Channel inChannel = ctx.channel();

    if(msg instanceof TextWebSocketFrame) {

    在writeflus之前加上这句之后,就可以了.