jboolean Java_com_test_Image_isImageFormat(JNIEnv* env, jobject obj, jstring path) { FILE *fis = NULL; jboolean ret = JNI_FALSE; unsigned short pis[128]; char cString[256]; const char *fileName = (*env)->GetStringUTFChars(env, path, 0); char *p = strrchr(fileName, "".""); if(p == NULL) { (*env)->ReleaseStringUTFChars(env, path, fileName); return JNI_FALSE; } short int i = 0; char *q = p; for(i = 0; i < strlen(q); i++) *(q+i) = tolower(*(q+i)); strcpy(cString, fileName); (*env)->ReleaseStringUTFChars(env, path, fileName); if((fis=fopen(cString, "r"))==NULL) { __android_log_print(ANDROID_LOG_ERROR, "isImageFormat", "=============Can not open the file=================="); fclose(fis); return JNI_FALSE; } fread(pis, 8, 1, fis); . . . } 首先要说的是,这函数会被频繁调用; |
|
10分 |
函数不全,怎么知道有没有内存泄漏呢。
fis这个句柄后面有关闭吗? 另外有几个问题: for(i = 0; i < strlen(q); i++) *(q+i) = tolower(*(q+i)); 2.没有字符串长度检查,可能导致内存写越界。 strcpy(cString, fileName); |
「fis这个句柄后面有关闭吗?」 这个后面关了的,就是执行到 if((fis=fopen(cString, "r"))==NULL) if语句就报错了 |
|
报错是指程序抛出异常,还是fopen返回NULL? |
|
10-21 04:22:47.479: E/Trace(6232): error opening trace file: No such file or directory (2) 10-21 04:22:47.493: W/ImageLoader(6232): Unable to create external cache directory 10-21 04:22:47.516: D/dalvikvm(6232): GC_FOR_ALLOC freed 86K, 3% free 4144K/4260K, paused 10ms, total 10ms 10-21 04:22:47.521: I/dalvikvm-heap(6232): Grow heap (frag case) to 7.586MB for 3686416-byte allocation 10-21 04:22:47.532: D/dalvikvm(6232): GC_FOR_ALLOC freed 1K, 2% free 7743K/7864K, paused 10ms, total 10ms 10-21 04:22:47.543: D/dalvikvm(6232): GC_CONCURRENT freed <1K, 2% free 7743K/7864K, paused 2ms+1ms, total 11ms 10-21 04:22:47.569: D/PathManager(6232): listFiles size=2 10-21 04:22:47.569: D/MainActivity(6232): mFiles 2 10-21 04:22:47.571: D/dalvikvm(6232): Trying to load lib /data/app-lib/com.test.picture-1/libtiff.so 0x4109cc88 10-21 04:22:47.572: D/dalvikvm(6232): Added shared lib /data/app-lib/com.test.picture-1/libtiff.so 0x4109cc88 10-21 04:22:47.573: D/dalvikvm(6232): No JNI_OnLoad found in /data/app-lib/com.test.picture-1/libtiff.so 0x4109cc88, skipping init 10-21 04:22:47.573: D/dalvikvm(6232): Trying to load lib /data/app-lib/com.test.picture-1/libtiffdecoder.so 0x4109cc88 10-21 04:22:47.574: D/dalvikvm(6232): Added shared lib /data/app-lib/com.test.picture-1/libtiffdecoder.so 0x4109cc88 10-21 04:22:47.574: D/dalvikvm(6232): No JNI_OnLoad found in /data/app-lib/com.test.picture-1/libtiffdecoder.so 0x4109cc88, skipping init 10-21 04:22:47.577: E/isImageFormat(6232): =============Can not open the file================== 10-21 04:22:47.577: A/libc(6232): Fatal signal 11 (SIGSEGV) at 0x0000000c (code=1), thread 6232 (.test.picture) 10-21 04:22:49.140: W/ImageLoader(6292): Unable to create external cache directory 10-21 04:22:49.169: D/dalvikvm(6292): GC_FOR_ALLOC freed 101K, 3% free 4144K/4272K, paused 11ms, total 12ms 10-21 04:22:49.175: I/dalvikvm-heap(6292): Grow heap (frag case) to 7.586MB for 3686416-byte allocation 10-21 04:22:49.186: D/dalvikvm(6292): GC_FOR_ALLOC freed 1K, 2% free 7743K/7876K, paused 11ms, total 11ms 10-21 04:22:49.197: D/dalvikvm(6292): GC_CONCURRENT freed <1K, 2% free 7743K/7876K, paused 2ms+1ms, total 11ms 10-21 04:22:49.232: D/PathManager(6292): listFiles size=2 10-21 04:22:49.232: D/MainActivity(6292): mFiles 2 10-21 04:22:49.234: D/dalvikvm(6292): Trying to load lib /data/app-lib/com.test.picture-1/libtiff.so 0x410a05b0 10-21 04:22:49.234: D/dalvikvm(6292): Added shared lib /data/app-lib/com.test.picture-1/libtiff.so 0x410a05b0 10-21 04:22:49.234: D/dalvikvm(6292): No JNI_OnLoad found in /data/app-lib/com.test.picture-1/libtiff.so 0x410a05b0, skipping init 10-21 04:22:49.235: D/dalvikvm(6292): Trying to load lib /data/app-lib/com.test.picture-1/libtiffdecoder.so 0x410a05b0 10-21 04:22:49.235: D/dalvikvm(6292): Added shared lib /data/app-lib/com.test.picture-1/libtiffdecoder.so 0x410a05b0 10-21 04:22:49.235: D/dalvikvm(6292): No JNI_OnLoad found in /data/app-lib/com.test.picture-1/libtiffdecoder.so 0x410a05b0, skipping init 10-21 04:22:49.238: E/isImageFormat(6292): =============Can not open the file================== 10-21 04:22:49.238: A/libc(6292): Fatal signal 11 (SIGSEGV) at 0x0000000c (code=1), thread 6292 (.test.picture) 就是判断到某个文件时, 就出错了. |
|
10-21 04:29:58.304: I/BootReceiver(1973): Copying /data/tombstones/tombstone_03 to DropBox (SYSTEM_TOMBSTONE) 10-21 04:29:58.310: D/Zygote(1306): Process 7684 terminated by signal (11) 10-21 04:29:58.340: D/dalvikvm(1973): GC_FOR_ALLOC freed 360K, 19% free 7530K/9208K, paused 36ms, total 36ms 10-21 04:29:58.342: I/ActivityManager(1973): Process com.test.picture (pid 7684) has died. 73): Process com.test.picture (pid 7699) has died. 10-21 04:29:58.780: D/Zygote(1306): Process 7699 terminated by signal (11) 10-21 04:29:58.784: D/dalvikvm(7714): Late-enabling CheckJNI 10-21 04:29:58.786: I/ActivityManager(1973): Start proc com.test.picture for activity com.test.picture/.MainActivity: pid=7714 uid=10058 gids={50058, 3003, 1015, 1028} 10-21 04:29:58.805: E/jdwp(7714): Failed sending reply to debugger: Broken pipe 10-21 04:29:58.805: D/dalvikvm(7714): Debugger has detached; object registry had 1 entries 10-21 04:29:58.810: E/Trace(7714): error opening trace file: No such file or directory (2) 10-21 04:29:58.823: W/ImageLoader(7714): Unable to create external cache directory 10-21 04:29:58.845: D/dalvikvm(7714): GC_FOR_ALLOC freed 96K, 3% free 4144K/4268K, paused 9ms, total 9ms 10-21 04:29:58.851: I/dalvikvm-heap(7714): Grow heap (frag case) to 7.586MB for 3686416-byte allocation 10-21 04:29:58.861: D/dalvikvm(7714): GC_FOR_ALLOC freed 1K, 2% free 7743K/7872K, paused 10ms, total 10ms 10-21 04:29:58.865: E/libhi_common(7728): [807158482 ERROR-HI_KEYLED]:HI_UNF_KEYLED_SelectType[160]:keyled select keyled type failed s32Result = 0xffffffff 10-21 04:29:58.875: D/dalvikvm(7714): GC_CONCURRENT freed <1K, 2% free 7743K/7872K, paused 4ms+1ms, total 14ms 10-21 04:29:58.911: D/PathManager(7714): listFiles size=2 10-21 04:29:58.911: D/MainActivity(7714): mFiles 2 10-21 04:29:58.913: D/dalvikvm(7714): Trying to load lib /data/app-lib/com.test.picture-1/libtiff.so 0x4109f0f0 10-21 04:29:58.914: D/dalvikvm(7714): Added shared lib /data/app-lib/com.test.picture-1/libtiff.so 0x4109f0f0 10-21 04:29:58.914: D/dalvikvm(7714): No JNI_OnLoad found in /data/app-lib/com.test.picture-1/libtiff.so 0x4109f0f0, skipping init 10-21 04:29:58.914: D/dalvikvm(7714): Trying to load lib /data/app-lib/com.test.picture-1/libtiffdecoder.so 0x4109f0f0 10-21 04:29:58.915: D/dalvikvm(7714): Added shared lib /data/app-lib/com.test.picture-1/libtiffdecoder.so 0x4109f0f0 10-21 04:29:58.915: D/dalvikvm(7714): No JNI_OnLoad found in /data/app-lib/com.test.picture-1/libtiffdecoder.so 0x4109f0f0, skipping init 10-21 04:29:58.917: E/isImageFormat(7714): =============Can not open the file================== 10-21 04:29:58.918: A/libc(7714): Fatal signal 11 (SIGSEGV) at 0x0000000c (code=1), thread 7714 (.test.picture) 这是全部的Log |
|
30分 |
if((fis=fopen(cString, "r"))==NULL) { __android_log_print(ANDROID_LOG_ERROR, "isImageFormat", "=============Can not open the file=================="); fclose(fis); return JNI_FALSE; } LZ都知道是这段代码出错了!很明显文件都没有打开成功,却去做关闭文件句柄的操作。。能不报错吗? |
被误导了。。。这那叫内存泄漏啊。
|