Skip to content

Commit

Permalink
Fix issue #140 (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
fwolff authored and florianf committed May 22, 2017
1 parent 87ed872 commit c7f383d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compiler/objc/src/main/java/org/robovm/objc/ObjCBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public final class ObjCBlock extends Struct<ObjCBlock> {

@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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}

Expand Down Expand Up @@ -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());
}
}

0 comments on commit c7f383d

Please sign in to comment.