From a2d6728708f5a6de9f8ce28713fe49f2c5d9259b Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 8 Mar 2023 04:48:58 +0000 Subject: [PATCH] [Java] Expose the finalizer of traits, as some traits are locks --- java_strings.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/java_strings.py b/java_strings.py index c44dca65..87ec4f86 100644 --- a/java_strings.py +++ b/java_strings.py @@ -777,12 +777,26 @@ import javax.annotation.Nullable; out_java_trait = out_java_trait + "\t\t" + var.from_hu_conv[1].replace("\n", "\n\t\t") + ";\n" else: out_java_trait = out_java_trait + "\t\tthis.ptrs_to.add(" + var[1] + ");\n" - out_java_trait = out_java_trait + "\t\tthis.bindings_instance = arg;\n" - out_java_trait = out_java_trait + "\t}\n" - out_java_trait = out_java_trait + "\t@Override @SuppressWarnings(\"deprecation\")\n" - out_java_trait = out_java_trait + "\tprotected void finalize() throws Throwable {\n" - out_java_trait = out_java_trait + "\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); } super.finalize();\n" - out_java_trait = out_java_trait + "\t}\n\n" + out_java_trait += f""" this.bindings_instance = arg; + }} + @Override @SuppressWarnings("deprecation") + protected void finalize() throws Throwable {{ + if (ptr != 0) {{ bindings.{struct_name.replace("LDK","")}_free(ptr); }} super.finalize(); + }} + /** + * Destroys the object, freeing associated resources. After this call, any access + * to this object may result in a SEGFAULT or worse. + * + * You should generally NEVER call this method. You should let the garbage collector + * do this for you when it finalizes objects. However, it may be useful for types + * which represent locks and should be closed immediately to avoid holding locks + * until the GC runs. + */ + public void destroy() {{ + if (ptr != 0) {{ bindings.{struct_name.replace("LDK","")}_free(ptr); }} + ptr = 0; + }} +""" java_trait_constr = "\tprivate static class " + struct_name + "Holder { " + struct_name.replace("LDK", "") + " held; }\n" java_trait_constr = java_trait_constr + "\tpublic static " + struct_name.replace("LDK", "") + " new_impl(" + struct_name.replace("LDK", "") + "Interface arg" -- 2.39.5