Skip to content

Commit

Permalink
Release weak ref too
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveCTurner committed Sep 6, 2021
1 parent bb75249 commit 5299bd5
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions native/dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -3525,8 +3525,14 @@ Java_com_sun_jna_Native_ffi_1prep_1closure(JNIEnv *env, jclass UNUSED(cls), jlon
}

cb->object = (*env)->NewWeakGlobalRef(env, obj);
if (cb->object == NULL) {
// either obj was null or an OutOfMemoryError has been thrown
free(cb);
return 0;
}
cb->closure = ffi_closure_alloc(sizeof(ffi_closure), L2A(&cb->x_closure));
if (cb->closure == NULL) {
(*env)->DeleteWeakGlobalRef(env, cb->object);
free(cb);
throwByName(env, EUnsupportedOperation, "Failed to allocate closure");
return 0;
Expand All @@ -3536,6 +3542,7 @@ Java_com_sun_jna_Native_ffi_1prep_1closure(JNIEnv *env, jclass UNUSED(cls), jlon
cb, cb->x_closure);
if (ffi_error(env, "ffi_prep_cif", s)) {
ffi_closure_free(cb->closure);
(*env)->DeleteWeakGlobalRef(env, cb->object);
free(cb);
return 0;
}
Expand Down

0 comments on commit 5299bd5

Please sign in to comment.