Giuseppe
Giuseppe

Reputation: 1089

Android : JNI Java From C Memory Leak

It seems that following code is wrong, as on the first call the code work well, but on second the VM kill the caller App.

 JNIEXPORT jint JNICALL Java_com_voice4facebook_MegaRecorderActivity_startMix(JNIEnv* env, jobject obj, jobjectArray params) {
int stringCount = (*env)->GetArrayLength(env, params);
char * argv[stringCount];
int i = 0;
while (i < stringCount) {
    jstring string = (jstring)(*env)->GetObjectArrayElement(env, params, i);

    const char *rawString = (*env)->GetStringUTFChars(env, string, 0);
    argv[i] = rawString;

    // Don't forget to call `ReleaseStringUTFChars` when you're done
    i++;
}

main(stringCount, argv);

i = 0;

while (i < stringCount)
{
    jstring string = (jstring)(*env)->GetObjectArrayElement(env, params, i);
    (*env)->ReleaseStringUTFChars (env, string, argv[i++]);
}

success = 1;
return 0;

}

Logcat:

02-14 16:18:18.075 26785 26835 D PowerManagerService: setLightBrightness : mButtonLight : 0
02-14 16:18:19.625 17098 17098 I DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
02-14 16:18:19.625 17098 17098 I DEBUG   : Build fingerprint: 'samsung/GT-I9100/GT-I9100:2.3.5/GINGERBREAD/XWKI8:user/release-keys'
02-14 16:18:19.625 17098 17098 I DEBUG   : pid: 21146, tid: 21247  >>> com.voice4facebook <<<
02-14 16:18:19.625 17098 17098 I DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr deadbaad
02-14 16:18:19.625 17098 17098 I DEBUG   :  r0 00000027  r1 deadbaad  r2 0000001c  r3 00000000
02-14 16:18:19.625 17098 17098 I DEBUG   :  r4 00000001  r5 00000000  r6 002f2ad8  r7 0000a000
02-14 16:18:19.625 17098 17098 I DEBUG   :  r8 8291f778  r9 47172eb4  10 48052a60  fp 48052ab4
02-14 16:18:19.625 17098 17098 I DEBUG   :  ip afd466a8  sp 480529d8  lr afd1a123  pc afd161c0  cpsr 60000030
02-14 16:18:19.635 17098 17098 I DEBUG   :  d0  459000003f800000  d1  0001000100001200
02-14 16:18:19.635 17098 17098 I DEBUG   :  d2  41d80000cf000000  d3  3f80000000ff00ff
02-14 16:18:19.635 17098 17098 I DEBUG   :  d4  41d8000000000000  d5  423400003fc00000
02-14 16:18:19.635 17098 17098 I DEBUG   :  d6  0000000043b40000  d7  000000024f000000
02-14 16:18:19.635 17098 17098 I DEBUG   :  d8  3ff0000000000000  d9  0000000000000000
02-14 16:18:19.635 17098 17098 I DEBUG   :  d10 0000000000000000  d11 0000000000000000
02-14 16:18:19.635 17098 17098 I DEBUG   :  d12 0000000000000000  d13 0000000000000000
02-14 16:18:19.640 17098 17098 I DEBUG   :  d14 0000000000000000  d15 0000000000000000
02-14 16:18:19.640 17098 17098 I DEBUG   :  d16 0000000000000000  d17 4000000000000000
02-14 16:18:19.640 17098 17098 I DEBUG   :  d18 8f94989898989898  d19 e1e9f0f0f0f0f0f0
02-14 16:18:19.640 17098 17098 I DEBUG   :  d20 0000000000000000  d21 0000000000000000
02-14 16:18:19.640 17098 17098 I DEBUG   :  d22 0000000000000000  d23 0000000000000000
02-14 16:18:19.640 17098 17098 I DEBUG   :  d24 0000000000000000  d25 0000000000000000
02-14 16:18:19.640 17098 17098 I DEBUG   :  d26 9898989898989898  d27 f0f0f0f0f0f0f0f0
02-14 16:18:19.640 17098 17098 I DEBUG   :  d28 0002aaa80002aaa8  d29 0002aaa80002aaa8
02-14 16:18:19.645 17098 17098 I DEBUG   :  d30 0001000000010000  d31 0001000000010000
02-14 16:18:19.650 17098 17098 I DEBUG   :  scr 60000013
02-14 16:18:19.650 17098 17098 I DEBUG   : 
02-14 16:18:19.780 17098 17098 I DEBUG   :          #00  pc 000161c0  /system/lib/libc.so
02-14 16:18:19.780 17098 17098 I DEBUG   :          #01  pc 00013b30  /system/lib/libc.so
02-14 16:18:19.780 17098 17098 I DEBUG   :          #02  pc 000149f6  /system/lib/libc.so
02-14 16:18:19.780 17098 17098 I DEBUG   :          #03  pc 00010f70  /data/data/com.voice4facebook/lib/libplayer.so
02-14 16:18:19.780 17098 17098 I DEBUG   :          #04  pc 00017ef4  /system/lib/libdvm.so
02-14 16:18:19.780 17098 17098 I DEBUG   :          #05  pc 0004994c  /system/lib/libdvm.so
02-14 16:18:19.785 17098 17098 I DEBUG   :          #06  pc 00042440  /system/lib/libdvm.so
02-14 16:18:19.785 17098 17098 I DEBUG   :          #07  pc 0004f0fe  /system/lib/libdvm.so
02-14 16:18:19.785 17098 17098 I DEBUG   :          #08  pc 00029790  /system/lib/libdvm.so
02-14 16:18:19.785 17098 17098 I DEBUG   : 
02-14 16:18:19.785 17098 17098 I DEBUG   : code around pc:
02-14 16:18:19.785 17098 17098 I DEBUG   : afd161a0 2c006824 e028d1fb b13368db c064f8df 
02-14 16:18:19.785 17098 17098 I DEBUG   : afd161b0 44fc2401 4000f8cc 49124798 25002027 
02-14 16:18:19.785 17098 17098 I DEBUG   : afd161c0 f7f57008 2106ea16 eb8af7f6 460aa901 
02-14 16:18:19.785 17098 17098 I DEBUG   : afd161d0 f04f2006 95015380 95029303 eef0f7f5 
02-14 16:18:19.785 17098 17098 I DEBUG   : afd161e0 462aa905 f7f52002 f7f5eefc 2106ea02 
02-14 16:18:19.785 17098 17098 I DEBUG   : 
02-14 16:18:19.785 17098 17098 I DEBUG   : code around lr:
02-14 16:18:19.785 17098 17098 I DEBUG   : afd1a100 f9b0d505 2100000e f7f12202 89a1ea4a 
02-14 16:18:19.785 17098 17098 I DEBUG   : afd1a110 f421462a 46315080 f9b481a0 f7f1000e 
02-14 16:18:19.785 17098 17098 I DEBUG   : afd1a120 bd70e9d0 4604b510 000ef9b0 e9b8f7f1 
02-14 16:18:19.785 17098 17098 I DEBUG   : afd1a130 bfbe2800 f42389a3 81a35380 6d21db02 
02-14 16:18:19.785 17098 17098 I DEBUG   : afd1a140 6523180b bf00bd10 41f3e92d f44f4606 
02-14 16:18:19.785 17098 17098 I DEBUG   : 
02-14 16:18:19.785 17098 17098 I DEBUG   : stack:
02-14 16:18:19.785 17098 17098 I DEBUG   :     48052998  afd4686c  
02-14 16:18:19.785 17098 17098 I DEBUG   :     4805299c  00313128  
02-14 16:18:19.785 17098 17098 I DEBUG   :     480529a0  0000001c  
02-14 16:18:19.785 17098 17098 I DEBUG   :     480529a4  afd187b9  /system/lib/libc.so
02-14 16:18:19.785 17098 17098 I DEBUG   :     480529a8  afd42644  
02-14 16:18:19.785 17098 17098 I DEBUG   :     480529ac  afd4686c  
02-14 16:18:19.785 17098 17098 I DEBUG   :     480529b0  00000000  
02-14 16:18:19.785 17098 17098 I DEBUG   :     480529b4  afd196f1  /system/lib/libc.so
02-14 16:18:19.785 17098 17098 I DEBUG   :     480529b8  00000001  
02-14 16:18:19.785 17098 17098 I DEBUG   :     480529bc  480529ec  
02-14 16:18:19.785 17098 17098 I DEBUG   :     480529c0  002f2ad8  
02-14 16:18:19.790 17098 17098 I DEBUG   :     480529c4  0000a000  
02-14 16:18:19.790 17098 17098 I DEBUG   :     480529c8  8291f778  
02-14 16:18:19.790 17098 17098 I DEBUG   :     480529cc  afd18a13  /system/lib/libc.so
02-14 16:18:19.790 17098 17098 I DEBUG   :     480529d0  df002777  
02-14 16:18:19.790 17098 17098 I DEBUG   :     480529d4  e3a070ad  
02-14 16:18:19.790 17098 17098 I DEBUG   : #00 480529d8  8291f778  
02-14 16:18:19.790 17098 17098 I DEBUG   :     480529dc  00000004  
02-14 16:18:19.790 17098 17098 I DEBUG   :     480529e0  000001c0  
02-14 16:18:19.790 17098 17098 I DEBUG   :     480529e4  00000000  
02-14 16:18:19.790 17098 17098 I DEBUG   :     480529e8  002f2ad8  
02-14 16:18:19.790 17098 17098 I DEBUG   :     480529ec  fffffbdf  
02-14 16:18:19.790 17098 17098 I DEBUG   :     480529f0  8291f778  
02-14 16:18:19.790 17098 17098 I DEBUG   :     480529f4  afd46490  
02-14 16:18:19.790 17098 17098 I DEBUG   :     480529f8  00000000  
02-14 16:18:19.790 17098 17098 I DEBUG   :     480529fc  afd13b35  /system/lib/libc.so
02-14 16:18:19.790 17098 17098 I DEBUG   : #01 48052a00  000001c0  
02-14 16:18:19.790 17098 17098 I DEBUG   :     48052a04  00000000  
02-14 16:18:19.790 17098 17098 I DEBUG   :     48052a08  ffffffff  
02-14 16:18:19.790 17098 17098 I DEBUG   :     48052a0c  0000000f  
02-14 16:18:19.790 17098 17098 I DEBUG   :     48052a10  8291f778  
02-14 16:18:19.790 17098 17098 I DEBUG   :     48052a14  afd149f9  /system/lib/libc.so
02-14 16:18:19.920  2597  2785 I ALSAModule: Terminated ALSA PLAYBACK device hifi
02-14 16:18:20.350  2597 21255 E yamaha::media::Parameters: SalesCode = HUI
02-14 16:18:20.510 17098 17098 I DEBUG   : dumpstate /data/log/dumpstate_app_native.txt
02-14 16:18:20.510 26785 26821 I BootReceiver: Copying /data/tombstones/tombstone_08 to DropBox (SYSTEM_TOMBSTONE)
02-14 16:18:20.515 21257 21257 I dumpstate: begin
02-14 16:18:22.255 12902 12902 D dalvikvm: GC_EXPLICIT freed 6K, 47% free 3243K/6023K, external 0K/0K, paused 66ms
02-14 16:18:22.945  2589  2709 D VoldCmdListener: asec list
02-14 16:18:23.735 26913 26913 I StatusBarPolicy: onSignalStrengthsChanged
02-14 16:18:23.735 26913 26913 D StatusBarPolicy: updateSignalStrength: numeric=22299, curOperator=3 ITA
02-14 16:18:24.110 21257 21257 I dumpstate: done
02-14 16:18:24.165 17098 17098 I DEBUG   : debuggerd committing suicide to free the zombie!
02-14 16:18:24.165 21322 21322 I DEBUG   : debuggerd: Sep 28 2011 20:51:05

Upvotes: 0

Views: 1185

Answers (2)

Michael Anderson
Michael Anderson

Reputation: 73490

The first entry in the log is a hint to what the problem could be

02-14 16:18:19.625 17098 17098 I DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), 
    fault addr deadbaad

That address looks like a debug memory address.

From wikipedia:

0xDEADBAAD ("dead bad") is used by the Android libc abort() function when native heap corruption is detected.

This usually means you've free'd or delete'd something you didn't malloc or new. Maybe inside your main function you're trying to delete variables in the input array?

Upvotes: 0

James M
James M

Reputation: 16718

You need to release the string when you're done with it. Try this before success = 1:

i = 0;

while (i < stringCount)
{
    jstring string = (jstring)(*env)->GetObjectArrayElement(env, params, i);
    (*env)->ReleaseStringUTFChars (env, string, argv[i++]);
}

Upvotes: 1

Related Questions