Only handle `null` objects when its allowed
[ldk-java] / src / main / java / org / ldk / structs / CustomMessageReader.java
index 889712a6188ee46c9defd2cdaf399e179553f75b..fa2720f187eddc1d64a71c3aba8fe7dc43eef8a9 100644 (file)
@@ -4,6 +4,7 @@ import org.ldk.impl.bindings;
 import org.ldk.enums.*;
 import org.ldk.util.*;
 import java.util.Arrays;
+import java.lang.ref.Reference;
 import javax.annotation.Nullable;
 
 /**
@@ -23,7 +24,19 @@ public class CustomMessageReader extends CommonBase {
        protected void finalize() throws Throwable {
                if (ptr != 0) { bindings.CustomMessageReader_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.CustomMessageReader_free(ptr); }
+               ptr = 0;
+       }
        public static interface CustomMessageReaderInterface {
                /**
                 * Decodes a custom message to `CustomMessageType`. If the given message type is known to the
@@ -39,7 +52,8 @@ public class CustomMessageReader extends CommonBase {
                impl_holder.held = new CustomMessageReader(new bindings.LDKCustomMessageReader() {
                        @Override public long read(short message_type, byte[] buffer) {
                                Result_COption_TypeZDecodeErrorZ ret = arg.read(message_type, buffer);
-                               long result = ret != null ? ret.ptr : 0;
+                               Reference.reachabilityFence(arg);
+                               long result = ret == null ? 0 : ret.clone_ptr();
                                return result;
                        }
                });
@@ -53,7 +67,10 @@ public class CustomMessageReader extends CommonBase {
         */
        public Result_COption_TypeZDecodeErrorZ read(short message_type, byte[] buffer) {
                long ret = bindings.CustomMessageReader_read(this.ptr, message_type, buffer);
-               if (ret >= 0 && ret < 1024) { return null; }
+               Reference.reachabilityFence(this);
+               Reference.reachabilityFence(message_type);
+               Reference.reachabilityFence(buffer);
+               if (ret >= 0 && ret <= 4096) { return null; }
                Result_COption_TypeZDecodeErrorZ ret_hu_conv = Result_COption_TypeZDecodeErrorZ.constr_from_ptr(ret);
                return ret_hu_conv;
        }