From 16a4c92d20ced007b2c62a2d5444550b5308f730 Mon Sep 17 00:00:00 2001 From: Noah Watkins Date: Thu, 25 Oct 2012 14:43:09 -0700 Subject: [PATCH] java: clean-up in finalize() Signed-off-by: Noah Watkins --- src/java/java/com/ceph/fs/CephMount.java | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/java/java/com/ceph/fs/CephMount.java b/src/java/java/com/ceph/fs/CephMount.java index 8dea7f6872c3a..d9c6f46996354 100644 --- a/src/java/java/com/ceph/fs/CephMount.java +++ b/src/java/java/com/ceph/fs/CephMount.java @@ -89,6 +89,30 @@ public class CephMount { */ static native void native_initialize(); + /* + * Controls clean-up synchronization between the constructor and finalize(). + * If native_ceph_create fails, then we want a call to finalize() to not + * attempt to clean-up native context, because there is none. + */ + private boolean initialized = false; + + /* + * Try to clean-up. First, unmount() will catch users who forget to do the + * unmount manually. Second, release() will destroy the entire context. It + * is safe to call release after a failure in unmount. + */ + protected void finalize() throws Throwable { + if (initialized) { + try { + unmount(); + } catch (Exception e) {} + try { + native_ceph_release(instance_ptr); + } catch (Exception e) {} + } + super.finalize(); + } + /** * Create a new CephMount with specific client id. * @@ -96,6 +120,7 @@ public class CephMount { */ public CephMount(String id) { native_ceph_create(this, id); + initialized = true; } private static synchronized native int native_ceph_create(CephMount mount, String id); -- 2.39.5