Android:JNI Java来自C内存泄漏

时间:2012-02-14 15:38:14

标签: android c++ android-ndk

似乎以下代码是错误的,因为在第一次调用时代码运行良好,但是第二次VM会终止调用者应用程序。

 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

2 个答案:

答案 0 :(得分:1)

完成后,您需要释放字符串。请在success = 1

之前尝试此操作
i = 0;

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

答案 1 :(得分:0)

日志中的第一个条目是提示问题可能是什么

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

该地址看起来像是调试内存地址。

来自wikipedia

  

当检测到本机堆损坏时,Android libc abort()函数使用0xDEADBAAD(&#34;死坏&#34;)。

这通常意味着您已经freedelete了解了mallocnew 。也许在你的main函数中你试图删除输入数组中的变量?