From 9a29e34bd297711acf050c7c6a70f3d9e00b3721 Mon Sep 17 00:00:00 2001 From: Kristoffer Sjogren Date: Mon, 21 Mar 2016 22:38:15 +0100 Subject: [PATCH] Dispose comparator on database close, #54 --- .../java/org/fusesource/lmdbjni/Database.java | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/lmdbjni/src/main/java/org/fusesource/lmdbjni/Database.java b/lmdbjni/src/main/java/org/fusesource/lmdbjni/Database.java index 7dac98f..a2ca2d5 100644 --- a/lmdbjni/src/main/java/org/fusesource/lmdbjni/Database.java +++ b/lmdbjni/src/main/java/org/fusesource/lmdbjni/Database.java @@ -37,6 +37,8 @@ public class Database extends NativeObject implements AutoCloseable { private final Env env; + private Callback comparatorCallback; + private Callback directComparatorCallback; Database(Env env, long self) { super(self); @@ -60,6 +62,14 @@ public class Database extends NativeObject implements AutoCloseable { */ @Override public void close() { + if (comparatorCallback != null) { + comparatorCallback.dispose(); + comparatorCallback = null; + } + if (directComparatorCallback != null) { + directComparatorCallback.dispose(); + directComparatorCallback = null; + } if (self != 0) { mdb_dbi_close(env.pointer(), self); self = 0; @@ -242,6 +252,7 @@ public EntryIterator seekBackward(Transaction tx, byte[] key) { public EntryIterator iterate(Transaction tx) { return iterate(tx, null, IteratorType.FORWARD); } + /** *

* Creates a backward sequential iterator from the last key. @@ -348,7 +359,7 @@ public int put(Transaction tx, DirectBuffer key, DirectBuffer value, int flags) /** * Just reserve space for data in the database, don't copy it. - * + * * @return a pointer to the reserved space. */ public DirectBuffer reserve(Transaction tx, DirectBuffer key, int size) { @@ -638,8 +649,11 @@ public Cursor openCursor(Transaction tx) { * @param comparator a byte array comparator */ public void setComparator(Transaction tx, Comparator comparator) { - Callback callback = new Callback(new ByteArrayComparator(comparator), "compare", 2); - JNI.mdb_set_compare(tx.pointer(), this.pointer(), callback.getAddress()); + if (comparatorCallback != null) { + comparatorCallback.dispose(); + } + comparatorCallback = new Callback(new ByteArrayComparator(comparator), "compare", 2); + JNI.mdb_set_compare(tx.pointer(), this.pointer(), comparatorCallback.getAddress()); } /** @@ -663,8 +677,11 @@ public void setComparator(Transaction tx, Comparator comparator) { * @param comparator a zero copy comparator */ public void setDirectComparator(Transaction tx, Comparator comparator) { - Callback callback = new Callback(new DirectBufferComparator(comparator), "compare", 2); - JNI.mdb_set_compare(tx.pointer(), this.pointer(), callback.getAddress()); + if (directComparatorCallback != null) { + directComparatorCallback.dispose(); + } + directComparatorCallback = new Callback(new DirectBufferComparator(comparator), "compare", 2); + JNI.mdb_set_compare(tx.pointer(), this.pointer(), directComparatorCallback.getAddress()); } private static final class ByteArrayComparator {