Anton Tananaev
Anton Tananaev

Reputation: 2523

Netty readBytes reference counting

I have code that looks something like this:

void decodeFragment(ByteBuf fragment) {
    fragment.readByte();
    ...
}

void decodeMessage(ByteBuf buf) {
    buf.readByte(); // header
    ...
    decodeFragment(buf.readBytes(20));
}

As far as I understand, new buffer from readBytes will have its own reference counting and I need to take care of it. Is there any alternative if I know that I won't need fragment for longer than the original buffer? Maybe readSlice is what I'm looking for? Documentation is not very clear.

Another related question. If I create Unpooled.wrappedBuffer do I need to take care of reference counting as well? What about Unpooled.copiedBuffer?

Upvotes: 1

Views: 208

Answers (1)

Norman Maurer
Norman Maurer

Reputation: 23557

If you don't want need an extra reference-count and can share or want to share the underlying storage slice or readSlice is the methods you are looking for.

And yes even for wrappdBuffer and copiedBuffer you will need to take care of releasing the buffer.

Upvotes: 1

Related Questions