javatcpnetty4

Netty, reading message results with IllegalReferenceCountgException refCnt: 0


I am new to netty and I try to receive byte response from a server (non-netty). However, I belvie I missundersteand the reference count. While reading the response I am running into the IllegalReferenceCountException: refCnt:0

What I do is not that complicated. I set up the client, then I send a message and I am awaiting a response.

My ResponseHandler looks like that:

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);
    }
}

while reading the response in channelRead the exception occurres. Why does it happen. Could someone explain it to me?


Solution

  • Okay, I solved the problem. Everything I need to do is add ByteToMessageDedcoder, otherwise the incoming response will never be allocated so it won't be accessible.