From 5b1eaf3ff1d968c045c7a211d8e86ebe9cd475f2 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sun, 25 Feb 2024 17:08:12 +0100 Subject: misc/jni: introduce macros for deleting references --- misc/jni.c | 28 +++++++++------------------- misc/jni.h | 12 +++++++++++- 2 files changed, 20 insertions(+), 20 deletions(-) (limited to 'misc') diff --git a/misc/jni.c b/misc/jni.c index 7024bb7ecd..d1f06a2d61 100644 --- a/misc/jni.c +++ b/misc/jni.c @@ -166,8 +166,7 @@ int mp_jni_exception_get_summary(JNIEnv *env, jthrowable exception, if (string) { name = mp_jni_jstring_to_utf_chars(env, string, log); - (*env)->DeleteLocalRef(env, string); - string = NULL; + MP_JNI_LOCAL_FREEP(&string); } jmethodID get_message_id = (*env)->GetMethodID(env, exception_class, "getMessage", "()Ljava/lang/String;"); @@ -188,8 +187,7 @@ int mp_jni_exception_get_summary(JNIEnv *env, jthrowable exception, if (string) { message = mp_jni_jstring_to_utf_chars(env, string, log); - (*env)->DeleteLocalRef(env, string); - string = NULL; + MP_JNI_LOCAL_FREEP(&string); } if (name && message) { @@ -208,17 +206,9 @@ done: talloc_free(name); talloc_free(message); - if (class_class) { - (*env)->DeleteLocalRef(env, class_class); - } - - if (exception_class) { - (*env)->DeleteLocalRef(env, exception_class); - } - - if (string) { - (*env)->DeleteLocalRef(env, string); - } + MP_JNI_LOCAL_FREEP(&class_class); + MP_JNI_LOCAL_FREEP(&exception_class); + MP_JNI_LOCAL_FREEP(&string); return ret; } @@ -238,7 +228,7 @@ int mp_jni_exception_check(JNIEnv *env, int logging, struct mp_log *log) char *message = NULL; int ret = mp_jni_exception_get_summary(env, exception, &message, log); - (*env)->DeleteLocalRef(env, exception); + MP_JNI_LOCAL_FREEP(&exception); if (ret < 0) return ret; @@ -277,7 +267,7 @@ int mp_jni_init_jfields(JNIEnv *env, void *jfields, global ? (*env)->NewGlobalRef(env, clazz) : clazz; if (global) - (*env)->DeleteLocalRef(env, clazz); + MP_JNI_LOCAL_FREEP(&clazz); continue; } @@ -365,9 +355,9 @@ int mp_jni_reset_jfields(JNIEnv *env, void *jfields, continue; if (global) { - (*env)->DeleteGlobalRef(env, clazz); + MP_JNI_GLOBAL_FREEP(&clazz); } else { - (*env)->DeleteLocalRef(env, clazz); + MP_JNI_LOCAL_FREEP(&clazz); } *(jclass*)jfield = NULL; diff --git a/misc/jni.h b/misc/jni.h index c9e4c28fe7..5a6ec9767e 100644 --- a/misc/jni.h +++ b/misc/jni.h @@ -29,7 +29,7 @@ #define MP_JNI_GET_ENV(obj) mp_jni_get_env((obj)->log) #define MP_JNI_EXCEPTION_CHECK() mp_jni_exception_check(env, 0, NULL) #define MP_JNI_EXCEPTION_LOG(obj) mp_jni_exception_check(env, 1, (obj)->log) -#define MP_JNI_DO(what, obj, method, ...) (*env)->what(env, obj, method, ##__VA_ARGS__) +#define MP_JNI_DO(what, obj, ...) (*env)->what(env, obj, ##__VA_ARGS__) #define MP_JNI_NEW(clazz, method, ...) MP_JNI_DO(NewObject, clazz, method, ##__VA_ARGS__) #define MP_JNI_CALL_INT(obj, method, ...) MP_JNI_DO(CallIntMethod, obj, method, ##__VA_ARGS__) #define MP_JNI_CALL_BOOL(obj, method, ...) MP_JNI_DO(CallBooleanMethod, obj, method, ##__VA_ARGS__) @@ -39,6 +39,16 @@ #define MP_JNI_GET_INT(obj, field) MP_JNI_DO(GetIntField, obj, field) #define MP_JNI_GET_LONG(obj, field) MP_JNI_DO(GetLongField, obj, field) #define MP_JNI_GET_BOOL(obj, field) MP_JNI_DO(GetBoolField, obj, field) +#define MP_JNI_LOCAL_FREEP(objp) do { \ + if (*(objp)) \ + MP_JNI_DO(DeleteLocalRef, *(objp)); \ + *(objp) = NULL; \ + } while (0) +#define MP_JNI_GLOBAL_FREEP(objp) do { \ + if (*(objp)) \ + MP_JNI_DO(DeleteGlobalRef, *(objp)); \ + *(objp) = NULL; \ + } while (0) /* * Attach permanently a JNI environment to the current thread and retrieve it. -- cgit v1.2.3