From 903d15b3f2e674835fd7b13009278d8b6e933370 Mon Sep 17 00:00:00 2001 From: Lee Rhodes Date: Thu, 11 Apr 2024 21:47:09 -0700 Subject: [PATCH] Fix memory leak. --- .../filters/bloomfilter/BloomFilterTest.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/test/java/org/apache/datasketches/filters/bloomfilter/BloomFilterTest.java b/src/test/java/org/apache/datasketches/filters/bloomfilter/BloomFilterTest.java index 14c679a8d..dedaf9db3 100644 --- a/src/test/java/org/apache/datasketches/filters/bloomfilter/BloomFilterTest.java +++ b/src/test/java/org/apache/datasketches/filters/bloomfilter/BloomFilterTest.java @@ -28,13 +28,14 @@ import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.common.SketchesReadOnlyException; import org.apache.datasketches.memory.Memory; +import org.apache.datasketches.memory.WritableHandle; import org.apache.datasketches.memory.WritableMemory; import org.testng.annotations.Test; public class BloomFilterTest { @Test - public void createNewFilterTest() { + public void createNewFilterTest() throws Exception { // construct the same filter multiple ways final long numItems = 4000; final double targetFpp = 0.01; @@ -50,12 +51,14 @@ public void createNewFilterTest() { assertFalse(bf1.isDirect()); assertFalse(bf1.isReadOnly()); - final WritableMemory wmem = WritableMemory.allocateDirect(sizeBytes).getWritable(); - final BloomFilter bf2 = new BloomFilter(numBits, numHashes, seed, wmem); - assertTrue(bf2.isEmpty()); - assertTrue(bf2.hasMemory()); - assertTrue(bf2.isDirect()); - assertFalse(bf2.isReadOnly()); + try (WritableHandle wh = WritableMemory.allocateDirect(sizeBytes)) { + final WritableMemory wmem = wh.getWritable(); + final BloomFilter bf2 = new BloomFilter(numBits, numHashes, seed, wmem); + assertTrue(bf2.isEmpty()); + assertTrue(bf2.hasMemory()); + assertTrue(bf2.isDirect()); + assertFalse(bf2.isReadOnly()); + } } @Test(expectedExceptions = SketchesArgumentException.class)