Skip to content

Commit d5d2087

Browse files
committed
Fix resource leak with cached update blocks
1 parent bb22930 commit d5d2087

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

src/main/java/io/luna/game/model/mob/Mob.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,6 @@ public ByteBuf acquireCachedBlock() {
312312
public boolean cacheBlockIfAbsent(ByteMessage msg) {
313313
ByteBuf cachedMsg = msg.getBuffer().retainedDuplicate();
314314
if (cachedBlock.compareAndSet(null, cachedMsg)) {
315-
if (this instanceof Npc && asNpc().getIndex() == 1)
316-
System.err.println("cached block , won by " + Thread.currentThread().getName() + ", refs: " + cachedMsg.refCnt());
317315
return true;
318316
}
319317
cachedMsg.release();
@@ -335,8 +333,6 @@ public boolean cacheBlockIfAbsent(ByteMessage msg) {
335333
public void clearCachedBlock() {
336334
ByteBuf old = cachedBlock.getAndSet(null);
337335
if (old != null) {
338-
if (this instanceof Npc && asNpc().getIndex() == 1)
339-
System.err.println("About to clear old block, refs: " + old.refCnt());
340336
old.release();
341337
}
342338
}

src/main/java/io/luna/game/model/mob/block/PlayerUpdateBlockSet.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ public final class PlayerUpdateBlockSet extends AbstractUpdateBlockSet<Player> {
3232
@Override
3333
public void addBlockSet(Player player, ByteMessage msg, UpdateState state) {
3434
// The block has already been encoded for someone this cycle; reuse it.
35-
ByteBuf cachedBlock = player.acquireCachedBlock();
36-
if (state == UpdateState.UPDATE_LOCAL && cachedBlock != null) {
37-
try {
38-
msg.putBytes(cachedBlock);
39-
return;
40-
} finally {
41-
cachedBlock.release();
35+
if (state == UpdateState.UPDATE_LOCAL) {
36+
ByteBuf cachedBlock = player.acquireCachedBlock();
37+
if (cachedBlock != null) {
38+
try {
39+
msg.putBytes(cachedBlock);
40+
return;
41+
} finally {
42+
cachedBlock.release();
43+
}
4244
}
4345
}
4446

0 commit comments

Comments
 (0)