Commit 345515a0 authored by Rohit Yadav's avatar Rohit Yadav Committed by Adrien Maglo
Browse files

Android libjni: Fix memory leak by releasing any previous media player...


Android libjni: Fix memory leak by releasing any previous media player instance before creating a new one
Signed-off-by: Adrien Maglo's avatarAdrien Maglo <magsoft@videolan.org>
parent c5e0bae0
...@@ -21,6 +21,22 @@ jint getMediaPlayer(JNIEnv *env, jobject thiz) ...@@ -21,6 +21,22 @@ jint getMediaPlayer(JNIEnv *env, jobject thiz)
return (*env)->GetIntField(env, thiz, fieldMP); return (*env)->GetIntField(env, thiz, fieldMP);
} }
jboolean releaseMediaPlayer(JNIEnv *env, jobject thiz)
{
jclass clazz = (*env)->GetObjectClass(env, thiz);
jfieldID fieldMP = (*env)->GetFieldID(env, clazz,
"mMediaPlayerInstance", "I");
jint mediaPlayer = (*env)->GetIntField(env, thiz, fieldMP);
if (mediaPlayer != 0)
{
libvlc_media_player_t *mp = (libvlc_media_player_t*) mediaPlayer;
libvlc_media_player_stop(mp);
libvlc_media_player_release(mp);
(*env)->SetIntField(env, thiz, fieldMP, 0);
}
return (mediaPlayer == 0);
}
/* Pointer to the Java virtual machine /* Pointer to the Java virtual machine
* Note: It's okay to use a static variable for the VM pointer since there * Note: It's okay to use a static variable for the VM pointer since there
* can only be one instance of this shared library in a single VM * can only be one instance of this shared library in a single VM
...@@ -65,19 +81,8 @@ void Java_vlc_android_LibVLC_nativeInit(JNIEnv *env, jobject thiz) ...@@ -65,19 +81,8 @@ void Java_vlc_android_LibVLC_nativeInit(JNIEnv *env, jobject thiz)
void Java_vlc_android_LibVLC_nativeDestroy(JNIEnv *env, jobject thiz) void Java_vlc_android_LibVLC_nativeDestroy(JNIEnv *env, jobject thiz)
{ {
releaseMediaPlayer(env, thiz);
jclass clazz = (*env)->GetObjectClass(env, thiz); jclass clazz = (*env)->GetObjectClass(env, thiz);
jfieldID fieldMP = (*env)->GetFieldID(env, clazz,
"mMediaPlayerInstance", "I");
jint mediaPlayer = (*env)->GetIntField(env, thiz, fieldMP);
if (mediaPlayer != 0)
{
libvlc_media_player_t *mp = (libvlc_media_player_t*) mediaPlayer;
libvlc_media_player_stop(mp);
libvlc_media_player_release(mp);
(*env)->SetIntField(env, thiz, fieldMP, 0);
}
jfieldID field = (*env)->GetFieldID(env, clazz, "mLibVlcInstance", "I"); jfieldID field = (*env)->GetFieldID(env, clazz, "mLibVlcInstance", "I");
jint libVlcInstance = (*env)->GetIntField(env, thiz, field); jint libVlcInstance = (*env)->GetIntField(env, thiz, field);
if (!libVlcInstance) if (!libVlcInstance)
...@@ -96,6 +101,9 @@ void Java_vlc_android_LibVLC_readMedia(JNIEnv *env, jobject thiz, ...@@ -96,6 +101,9 @@ void Java_vlc_android_LibVLC_readMedia(JNIEnv *env, jobject thiz,
jboolean isCopy; jboolean isCopy;
const char *psz_mrl = (*env)->GetStringUTFChars(env, mrl, &isCopy); const char *psz_mrl = (*env)->GetStringUTFChars(env, mrl, &isCopy);
/* Release previous media player, if any */
releaseMediaPlayer(env, thiz);
/* Create a new item */ /* Create a new item */
libvlc_media_t *m = libvlc_media_new_path((libvlc_instance_t*)instance, libvlc_media_t *m = libvlc_media_new_path((libvlc_instance_t*)instance,
psz_mrl); psz_mrl);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment