[Android] libvlcjni: free JNI references

Louis Régnier git at videolan.org
Wed May 6 11:16:48 CEST 2020


vlc-android | branch: master | Louis Régnier <louis at videolabs.io> | Mon May  4 15:40:25 2020 +0200| [0aefcd0316f2781da4d9c7d52af41e3f098ecf39] | committer: Nicolas Pomepuy

libvlcjni: free JNI references

Inside JNI_OnLoad function, macro GET_CLASS leak a JNI LocalRef when the macro
get a GlobalRef and assign it, the LocalRef is lost.

JNI_OnLoad acquire many GlobalRef, but they are not all released in JNI_UnLoad.

> https://code.videolan.org/videolan/vlc-android/commit/0aefcd0316f2781da4d9c7d52af41e3f098ecf39
---

 libvlc/jni/libvlcjni.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/libvlc/jni/libvlcjni.c b/libvlc/jni/libvlcjni.c
index c8e4d59fc..9623dbb6b 100644
--- a/libvlc/jni/libvlcjni.c
+++ b/libvlc/jni/libvlcjni.c
@@ -124,7 +124,9 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
         return -1; \
     } \
     if (b_globlal) { \
+        jclass local_class = (clazz); \
         (clazz) = (jclass) (*env)->NewGlobalRef(env, (clazz)); \
+        (*env)->DeleteLocalRef(env, local_class); \
         if (!(clazz)) { \
             LOGE("NewGlobalRef(%s) failed", (str)); \
             return -1; \
@@ -147,6 +149,7 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
     GET_ID(GetStaticFieldID, SDK_INT_fieldID, Version_clazz, "SDK_INT", "I");
     fields.SDK_INT = (*env)->GetStaticIntField(env, Version_clazz,
                                                SDK_INT_fieldID);
+    (*env)->DeleteLocalRef(env, Version_clazz);
 
     GET_CLASS(fields.IllegalStateException.clazz,
               "java/lang/IllegalStateException", true);
@@ -348,8 +351,21 @@ void JNI_OnUnload(JavaVM* vm, void* reserved)
     (*env)->DeleteGlobalRef(env, fields.RuntimeException.clazz);
     (*env)->DeleteGlobalRef(env, fields.OutOfMemoryError.clazz);
     (*env)->DeleteGlobalRef(env, fields.String.clazz);
+    (*env)->DeleteGlobalRef(env, fields.FileDescriptor.clazz);
     (*env)->DeleteGlobalRef(env, fields.VLCObject.clazz);
     (*env)->DeleteGlobalRef(env, fields.Media.clazz);
+    (*env)->DeleteGlobalRef(env, fields.Media.Track.clazz);
+    (*env)->DeleteGlobalRef(env, fields.Media.Slave.clazz);
+    (*env)->DeleteGlobalRef(env, fields.MediaPlayer.clazz);
+    (*env)->DeleteGlobalRef(env, fields.MediaPlayer.Title.clazz);
+    (*env)->DeleteGlobalRef(env, fields.MediaPlayer.Chapter.clazz);
+    (*env)->DeleteGlobalRef(env, fields.MediaPlayer.TrackDescription.clazz);
+    (*env)->DeleteGlobalRef(env, fields.MediaPlayer.Equalizer.clazz);
+    (*env)->DeleteGlobalRef(env, fields.MediaDiscoverer.clazz);
+    (*env)->DeleteGlobalRef(env, fields.MediaDiscoverer.Description.clazz);
+    (*env)->DeleteGlobalRef(env, fields.RendererDiscoverer.clazz);
+    (*env)->DeleteGlobalRef(env, fields.RendererDiscoverer.Description.clazz);
+    (*env)->DeleteGlobalRef(env, fields.Dialog.clazz);
 
     pthread_key_delete(jni_env_key);
 



More information about the Android mailing list