X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fldk%2Fstructs%2FChannelMessageHandler.java;fp=src%2Fmain%2Fjava%2Forg%2Fldk%2Fstructs%2FChannelMessageHandler.java;h=060133855ae1117c56add57e5cf0a659e0bf9cdf;hb=fed2245c60159f6c074c9ed5c0f3ce273ad9841b;hp=ac0e308e5e53a823193ed8243709b1166459f86a;hpb=e9c2970adecaa14b3e83e11eb160a7bcbdae20da;p=ldk-java diff --git a/src/main/java/org/ldk/structs/ChannelMessageHandler.java b/src/main/java/org/ldk/structs/ChannelMessageHandler.java index ac0e308e..06013385 100644 --- a/src/main/java/org/ldk/structs/ChannelMessageHandler.java +++ b/src/main/java/org/ldk/structs/ChannelMessageHandler.java @@ -47,11 +47,13 @@ public class ChannelMessageHandler extends CommonBase { impl_holder.held = new ChannelMessageHandler(new bindings.LDKChannelMessageHandler() { @Override public void handle_open_channel(byte[] their_node_id, long their_features, long msg) { InitFeatures their_features_hu_conv = new InitFeatures(null, their_features); + their_features_hu_conv.ptrs_to.add(this); OpenChannel msg_hu_conv = new OpenChannel(null, msg); arg.handle_open_channel(their_node_id, their_features_hu_conv, msg_hu_conv); } @Override public void handle_accept_channel(byte[] their_node_id, long their_features, long msg) { InitFeatures their_features_hu_conv = new InitFeatures(null, their_features); + their_features_hu_conv.ptrs_to.add(this); AcceptChannel msg_hu_conv = new AcceptChannel(null, msg); arg.handle_accept_channel(their_node_id, their_features_hu_conv, msg_hu_conv); } @@ -128,12 +130,28 @@ public class ChannelMessageHandler extends CommonBase { public void handle_open_channel(byte[] their_node_id, InitFeatures their_features, OpenChannel msg) { bindings.ChannelMessageHandler_handle_open_channel(this.ptr, their_node_id, their_features == null ? 0 : their_features.ptr & ~1, msg == null ? 0 : msg.ptr & ~1); this.ptrs_to.add(their_features); + // Due to rust's strict-ownership memory model, in some cases we need to "move" + // an object to pass exclusive ownership to the function being called. + // In most cases, we avoid this being visible in GC'd languages by cloning the object + // at the FFI layer, creating a new object which Rust can claim ownership of + // However, in some cases (eg here), there is no way to clone an object, and thus + // we actually have to pass full ownership to Rust. + // Thus, after this call, their_features is reset to null and is now a dummy object. + their_features.ptr = 0; this.ptrs_to.add(msg); } public void handle_accept_channel(byte[] their_node_id, InitFeatures their_features, AcceptChannel msg) { bindings.ChannelMessageHandler_handle_accept_channel(this.ptr, their_node_id, their_features == null ? 0 : their_features.ptr & ~1, msg == null ? 0 : msg.ptr & ~1); this.ptrs_to.add(their_features); + // Due to rust's strict-ownership memory model, in some cases we need to "move" + // an object to pass exclusive ownership to the function being called. + // In most cases, we avoid this being visible in GC'd languages by cloning the object + // at the FFI layer, creating a new object which Rust can claim ownership of + // However, in some cases (eg here), there is no way to clone an object, and thus + // we actually have to pass full ownership to Rust. + // Thus, after this call, their_features is reset to null and is now a dummy object. + their_features.ptr = 0; this.ptrs_to.add(msg); }