From 9b1d3f63933c38becf5c5f603722782a0f6de6bd Mon Sep 17 00:00:00 2001 From: Peter Lawrey Date: Thu, 7 May 2020 11:33:10 +0100 Subject: [PATCH] Add a RandomAccessFile which doesn't resource leak on a GC, https://github.com/OpenHFT/Chronicle-Core/issues/108 --- .../chronicle/hash/impl/util/CanonicalRandomAccessFiles.java | 3 ++- src/test/java/net/openhft/chronicle/set/Builder.java | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/openhft/chronicle/hash/impl/util/CanonicalRandomAccessFiles.java b/src/main/java/net/openhft/chronicle/hash/impl/util/CanonicalRandomAccessFiles.java index 311815e04..02d2ce273 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/util/CanonicalRandomAccessFiles.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/util/CanonicalRandomAccessFiles.java @@ -16,6 +16,7 @@ package net.openhft.chronicle.hash.impl.util; +import net.openhft.chronicle.core.CleaningRandomAccessFile; import net.openhft.chronicle.core.Jvm; import java.io.File; @@ -36,7 +37,7 @@ public final class CanonicalRandomAccessFiles { return canonicalRafs.compute(file, (f, ref) -> { if (ref == null) { try { - return new RafReference(new RandomAccessFile(f, "rw")); + return new RafReference(new CleaningRandomAccessFile(f, "rw")); } catch (FileNotFoundException e) { throw Jvm.rethrow(e); } diff --git a/src/test/java/net/openhft/chronicle/set/Builder.java b/src/test/java/net/openhft/chronicle/set/Builder.java index bed10ca9e..2edde079c 100644 --- a/src/test/java/net/openhft/chronicle/set/Builder.java +++ b/src/test/java/net/openhft/chronicle/set/Builder.java @@ -44,8 +44,9 @@ public class Builder { This will only work if the user is an admin on windows. */ file.setWritable(true);//just in case relative path was used. - RandomAccessFile raf = new RandomAccessFile(file, "rw"); - raf.close();//allows closing the file access on windows. forcing to close access. Only works for admin-access. + try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) { + //allows closing the file access on windows. forcing to close access. Only works for admin-access. + } } //file.delete(); //isnt guaranteed on windows.