Skip to content

Commit 2d76cbb

Browse files
committed
#56 Possible fix for memory leak detected in logs.
1 parent 7534eca commit 2d76cbb

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

nadclient/src/main/java/io/nadron/client/handlers/netty/EventObjectDecoder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import io.nadron.client.event.Events;
44
import io.netty.buffer.ByteBuf;
5-
import io.netty.buffer.Unpooled;
65
import io.netty.channel.ChannelHandlerContext;
76
import io.netty.handler.codec.MessageToMessageDecoder;
87
import io.netty.handler.codec.serialization.ClassResolvers;
@@ -24,7 +23,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in,
2423
{
2524
opcode = Events.SESSION_MESSAGE;
2625
}
27-
ByteBuf data = Unpooled.buffer(in.readableBytes()).writeBytes(in);
26+
ByteBuf data = in.readBytes(in.readableBytes());
2827
// TODO check if creating a new object is necessary each time
2928
Object obj = new SourceDecoder().decode(ctx, data);
3029
out.add(Events.event(obj, opcode));

nadclient/src/main/java/io/nadron/client/handlers/netty/MessageBufferEventDecoder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import io.nadron.client.event.Event;
55
import io.nadron.client.event.Events;
66
import io.netty.buffer.ByteBuf;
7-
import io.netty.buffer.Unpooled;
87
import io.netty.channel.ChannelHandlerContext;
98
import io.netty.handler.codec.ByteToMessageDecoder;
109

@@ -41,7 +40,7 @@ protected Event decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception
4140
{
4241
opcode = Events.SESSION_MESSAGE;
4342
}
44-
ByteBuf data = Unpooled.buffer(in.readableBytes()).writeBytes(in);
43+
ByteBuf data = in.readBytes(in.readableBytes());
4544
return Events.event(new NettyMessageBuffer(data), opcode);
4645
}
4746
return null;

nadron/src/main/java/io/nadron/handlers/netty/MessageBufferEventDecoder.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import io.nadron.event.Event;
55
import io.nadron.event.Events;
66
import io.netty.buffer.ByteBuf;
7-
import io.netty.buffer.Unpooled;
87
import io.netty.channel.ChannelHandler.Sharable;
98
import io.netty.channel.ChannelHandlerContext;
109
import io.netty.handler.codec.MessageToMessageDecoder;
@@ -33,14 +32,13 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buffer,
3332
out.add(decode(ctx, buffer));
3433
}
3534

36-
public Event decode(ChannelHandlerContext ctx, ByteBuf buffer){
37-
byte opcode = buffer.readByte();
35+
public Event decode(ChannelHandlerContext ctx, ByteBuf in){
36+
byte opcode = in.readByte();
3837
if (opcode == Events.NETWORK_MESSAGE)
3938
{
4039
opcode = Events.SESSION_MESSAGE;
4140
}
42-
ByteBuf data = Unpooled.buffer(buffer.readableBytes()).writeBytes(
43-
buffer);
41+
ByteBuf data = in.readBytes(in.readableBytes());
4442
return Events.event(new NettyMessageBuffer(data), opcode);
4543
}
4644
}

nadron/src/test/java/io/nadron/AppTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,20 @@ public void readWriteSocketAddress()
5858
InetSocketAddress readAddress = NettyUtils.readSocketAddress(buffer);
5959
assertEquals(socketAddress,readAddress);
6060
}
61+
62+
@Test
63+
public void readWriteEmptyString()
64+
{
65+
String hello = "Hello";
66+
String world = "World!";
67+
String empty = "";
68+
String space = " ";
69+
ByteBuf buffer = NettyUtils.writeStrings(hello, empty, space, world);
70+
String[] readStrings = NettyUtils.readStrings(buffer, 4);
71+
assertEquals(readStrings.length, 4);
72+
assertEquals(readStrings[0], hello);
73+
assertEquals(readStrings[1], empty);
74+
assertEquals(readStrings[2], space);
75+
assertEquals(readStrings[3], world);
76+
}
6177
}

0 commit comments

Comments
 (0)