2016-06-07 53 views
2

我在擴展ReplayingDecoder的DecodeEventHandler類中遇到ResourceLeakDetector異常。我很難理解何時何地我應該釋放任何方法的BytBuf。我需要釋放傳遞的ByteBuf嗎?我試着釋放header,eventBody和attachedData ByteBuf對象,但後來在我的代碼中創建了問題。該代碼是...ReplayingDecoder中的ResourceLeakDetector異常。我何時發佈ByteBuf?

protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception 
{ 
    switch (state()) 
    { 
     case READ_HEADER: 
      ByteBuf header = byteBuf.readBytes(BaseEvent.EVENT_HEADER_SIZE); 
      newEvent = new BaseEvent(header.nioBuffer()); 
      checkpoint(EventDecoderState.READ_BODY); 
      // Fall through 

     case READ_BODY: 
      ByteBuf eventBody = byteBuf.readBytes(newEvent.getEventHeaderBodySize() - BaseEvent.EVENT_HEADER_SIZE); 
      newEvent.setEventBody(eventBody.nioBuffer()); 
      checkpoint(EventDecoderState.READ_ATTACHED_DATA); 
      // Fall through 

     case READ_ATTACHED_DATA: 
      ByteBuf attachedData = byteBuf.readBytes(newEvent.getAttachedDataSize()); 
      newEvent.clearAttachedData(); 
      newEvent.addAttachedData(attachedData.nioBuffer()); 
      list.add(newEvent); 
      checkpoint(EventDecoderState.READ_HEADER); 
    } 
} 

該方法開始於23行的異常我的接收是...

2016-06-07 13:30:49.783 [ERROR] (nioEventLoopGroup-7-1) io.netty.util.ResourceLeakDetector - LEAK: ByteBuf.release() was not called before it's garbage-collected. See http://netty.io/wiki/reference-counted-objects.html for more information. 
Recent access records: 2 
#2: 
    io.netty.buffer.AdvancedLeakAwareByteBuf.nioBuffer(AdvancedLeakAwareByteBuf.java:669) 
    com.oakgate.netty.DecodeEventHandler.decode(DecodeEventHandler.java:30) 
    io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:376) 
    io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:245) 
    io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelReadNow(ChannelHandlerInvokerUtil.java:83) 
    io.netty.channel.DefaultChannelHandlerInvoker.invokeChannelRead(DefaultChannelHandlerInvoker.java:154) 
    io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:354) 
    io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:145) 
    io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86) 
    io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelReadNow(ChannelHandlerInvokerUtil.java:83) 
    io.netty.channel.DefaultChannelHandlerInvoker.invokeChannelRead(DefaultChannelHandlerInvoker.java:154) 
    io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:354) 
    io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:145) 
    io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:266) 
    io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelReadNow(ChannelHandlerInvokerUtil.java:83) 
    io.netty.channel.DefaultChannelHandlerInvoker.invokeChannelRead(DefaultChannelHandlerInvoker.java:154) 
    io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:354) 
    io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:145) 
    io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:1078) 
    io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:117) 
    io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:527) 
    io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:484) 
    io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:398) 
    io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:370) 
    io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:742) 
    io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:145) 
    java.lang.Thread.run(Thread.java:745) 
#1: 
    io.netty.buffer.AdvancedLeakAwareByteBuf.writeBytes(AdvancedLeakAwareByteBuf.java:561) 
    io.netty.buffer.AbstractByteBuf.readBytes(AbstractByteBuf.java:790) 
    io.netty.handler.codec.ReplayingDecoderByteBuf.readBytes(ReplayingDecoderByteBuf.java:576) 
    com.oakgate.netty.DecodeEventHandler.decode(DecodeEventHandler.java:29) 
    io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:376) 
    io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:245) 
    io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelReadNow(ChannelHandlerInvokerUtil.java:83) 
    io.netty.channel.DefaultChannelHandlerInvoker.invokeChannelRead(DefaultChannelHandlerInvoker.java:154) 
    io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:354) 
    io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:145) 
    io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86) 
    io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelReadNow(ChannelHandlerInvokerUtil.java:83) 
    io.netty.channel.DefaultChannelHandlerInvoker.invokeChannelRead(DefaultChannelHandlerInvoker.java:154) 
    io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:354) 
    io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:145) 
    io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:266) 
    io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelReadNow(ChannelHandlerInvokerUtil.java:83) 
    io.netty.channel.DefaultChannelHandlerInvoker.invokeChannelRead(DefaultChannelHandlerInvoker.java:154) 
    io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:354) 
    io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:145) 
    io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:1078) 
    io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:117) 
    io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:527) 
    io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:484) 
    io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:398) 
    io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:370) 
    io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:742) 
    io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:145) 
    java.lang.Thread.run(Thread.java:745) 
Created at: 
    io.netty.buffer.PooledByteBufAllocator.newDirectBuffer(PooledByteBufAllocator.java:271) 
    io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:179) 
    io.netty.buffer.AbstractByteBufAllocator.buffer(AbstractByteBufAllocator.java:115) 
    io.netty.buffer.AbstractByteBuf.readBytes(AbstractByteBuf.java:789) 
    io.netty.handler.codec.ReplayingDecoderByteBuf.readBytes(ReplayingDecoderByteBuf.java:576) 
    com.oakgate.netty.DecodeEventHandler.decode(DecodeEventHandler.java:29) 
    io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:376) 
    io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:245) 
    io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelReadNow(ChannelHandlerInvokerUtil.java:83) 
    io.netty.channel.DefaultChannelHandlerInvoker.invokeChannelRead(DefaultChannelHandlerInvoker.java:154) 
    io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:354) 
    io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:145) 
    io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86) 
    io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelReadNow(ChannelHandlerInvokerUtil.java:83) 
    io.netty.channel.DefaultChannelHandlerInvoker.invokeChannelRead(DefaultChannelHandlerInvoker.java:154) 
    io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:354) 
    io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:145) 
    io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:266) 
    io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelReadNow(ChannelHandlerInvokerUtil.java:83) 
    io.netty.channel.DefaultChannelHandlerInvoker.invokeChannelRead(DefaultChannelHandlerInvoker.java:154) 
    io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:354) 
    io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:145) 
    io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:1078) 
    io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:117) 
    io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:527) 
    io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:484) 
    io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:398) 
    io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:370) 
    io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:742) 
    io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:145) 
    java.lang.Thread.run(Thread.java:745) 

附加信息

當我釋放創建的每個ByteBuf通過調用byteBuf.readBytes()(header,eventBody和attachedData),我在事件處理後遇到問題。我調用nioBuffer()從ByteBuf創建一個ByteBuffer,看起來釋放原始的ByteBuf會影響來自ioBuffer()調用的ByteBuffer。根據文檔,返回的緩衝區與bytebuf共享內容,這很可能意味着我無法釋放它。我是否需要保留每個ByteBuf,然後在事件處理時釋放?除非有其他方法可以做到這一點,否則就會出現這種情況。

回答

1

您正在通過byteBuf.readBytes創建新的ByteBuf s,它們需要發佈。

case READ_HEADER:

例子:

case READ_HEADER: 
    ByteBuf header = null; 
    try { 
     header = byteBuf.readBytes(BaseEvent.EVENT_HEADER_SIZE); 
     newEvent = new BaseEvent(header.nioBuffer()); 
     checkpoint(EventDecoderState.READ_BODY); 
    } finally { 
     if(header != null) 
      header.release(); 
    } 
    // Fall through 
+0

感謝您的答覆。我添加了額外的信息,描述實施建議時發生的情況。 –