diff --git a/compiler/objc/src/main/java/org/robovm/objc/ObjCBlock.java b/compiler/objc/src/main/java/org/robovm/objc/ObjCBlock.java index bf02d7bd4..25ee96f06 100755 --- a/compiler/objc/src/main/java/org/robovm/objc/ObjCBlock.java +++ b/compiler/objc/src/main/java/org/robovm/objc/ObjCBlock.java @@ -80,6 +80,10 @@ public final class ObjCBlock extends Struct { @StructMember(6) public native ObjCBlock wrapper_addr(@Pointer long wrapper_addr); + + public static void setHandle(ObjCBlock block, long handle) { + block.setHandle(handle); + } public Object object() { return VM.castAddressToObject(object_addr()); diff --git a/compiler/objc/src/main/java/org/robovm/objc/RunnableAsObjCBlockMarshaler.java b/compiler/objc/src/main/java/org/robovm/objc/RunnableAsObjCBlockMarshaler.java index 307b6a328..c41ddd679 100755 --- a/compiler/objc/src/main/java/org/robovm/objc/RunnableAsObjCBlockMarshaler.java +++ b/compiler/objc/src/main/java/org/robovm/objc/RunnableAsObjCBlockMarshaler.java @@ -31,6 +31,9 @@ */ public class RunnableAsObjCBlockMarshaler implements Runnable { + private static final Selector COPY_SELECTOR = Selector.register("copy"); + private static final Selector RELEASE_SELECTOR = Selector.register("release"); + private static ObjCBlock.Wrapper WRAPPER = new ObjCBlock.Wrapper(RunnableAsObjCBlockMarshaler.class); @@ -49,6 +52,8 @@ public static Runnable toObject(Class cls, long handle, long flags) { if (block.hasObject()) { return (Runnable) block.object(); } + handle = ObjCRuntime.ptr_objc_msgSend(handle, COPY_SELECTOR.getHandle()); + ObjCBlock.setHandle(block, handle); return new RunnableAsObjCBlockMarshaler(block); } @@ -128,4 +133,9 @@ public void run() { private static void invoked(ObjCBlock block) { ((Runnable) block.object()).run(); } + + @Override + protected void finalize() throws Throwable { + ObjCRuntime.void_objc_msgSend(this.objCBlock.getHandle(), RELEASE_SELECTOR.getHandle()); + } }