Netty,读取消息结果带有IllegalReferenceCountgException refCnt: 0

Netty, reading message results with IllegalReferenceCountgException refCnt: 0

我是 netty 的新手,我尝试从服务器(非 netty)接收字节响应。但是,我相信我误解了引用计数。在阅读回复时,我 运行 进入了 IllegalReferenceCountException: refCnt:0

我做的事情没有那么复杂。我设置了客户端,然后我发送了一条消息,我正在等待响应。

我的 ResponseHandler 看起来像这样:

public class ResponseHandler extends DriverResponseHandler
{
    private ByteBuf received;


    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception
    {
        super.handlerAdded(ctx);
        received = ctx.alloc().buffer(32);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
    {
        super.channelRead(ctx, msg);
        received.writeBytes((ByteBuf) msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception
    {
        super.channelReadComplete(ctx);
        if (received.writerIndex() == received.capacity() || received.readerIndex() == received.capacity()) {
            received.clear();
            received = ctx.alloc().buffer(32);
        }
        processResponse(received);
    }
}

在读取 channelRead 中的响应时发生异常。为什么会这样。有人可以给我解释一下吗?

好的,我解决了这个问题。我需要做的就是添加 ByteToMessageDedcoder,否则永远不会分配传入的响应,因此无法访问它。