Memory Leaks in Android Native Code [C/C++]

926 views
Skip to first unread message

Sunil Mourya

unread,
Dec 8, 2013, 11:39:24 PM12/8/13
to andro...@googlegroups.com
I introduced a memory leak in "android_media_MediaPlayer.cpp" of android but didn't freed that memory. Please find below function where I introduced memory leak.

static void android_media_MediaPlayer_native_setup(JNIEnv *env, jobject thiz, jobject weak_this)
{
  ALOGV("native_setup");
  sp<MediaPlayer> mp = new MediaPlayer();

  ALOGV("MEMORY LEAK - MediaPlayer");
  char *MemoryLeak_MediaPlayer = (char *)malloc(100000);

  if(mp == NULL) {
    jniThrowException(env, "java/lang/RuntimeException", "Out of memory");
    return;
  }
  // create new listener and give it to MediaPlayer
  sp<JNIMediaPlayerListener> listener = new JNIMediaPlayerListener(env, thiz, weak_this);
  mp->setListener(listener);
  // Stow our new C++ MediaPlayer in an opaque field in the Java object.
  setMediaPlayer(env, thiz, mp);
}
How to use VALGRIND to find out Memory Leak and Memory Corruption in Android Native Code? If anyone is using some other tool, please explain the procedure.

Baodong Chen

unread,
Dec 9, 2013, 11:46:43 PM12/9/13
to andro...@googlegroups.com
the malloc(100000) function call would be optimized and removed by compiler, i guess


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-ndk.
For more options, visit https://groups.google.com/groups/opt_out.

NoAngel

unread,
Dec 10, 2013, 5:57:05 AM12/10/13
to andro...@googlegroups.com
If u just malloced and not using memory after, OS may reserve xxx bytes in virtual memory and not even use physical memory.
try to write to memory after malloc, it will force OS to perform physical memory allocation.

Vladimir Kunschikov

unread,
Dec 10, 2013, 8:51:40 AM12/10/13
to andro...@googlegroups.com
Usage of valgrind is quite the same as on x86.  You  should run mediaserver under valgrind:
#killl `pidof mediaserver`
#/data/local/Inst/valgrind --log-file=/tmp/valgrind.log --leak-check=full /system/bin/mediaserver
..call this code ..
#kill `pidof mediaserver`

pull and read /tmp/valgrind.log
Reply all
Reply to author
Forward
0 new messages