在android裏作ndk編程的時候,碰到個隨機性錯誤java
錯誤信息以下:android
05-06 15:59:44.411: A/libc(3347): Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1)
05-06 15:59:44.911: I/DEBUG(3344): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
05-06 15:59:44.911: I/DEBUG(3344): Build fingerprint: 'i.Kan/full_godbox/godbox:4.0.3/IML74K/eng.mipt.20130428.110435:eng/test-keys'
05-06 15:59:44.911: I/DEBUG(3344): pid: 3347, tid: 3348 >>> com.nef.xxx <<<
05-06 15:59:44.911: I/DEBUG(3344): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr deadbaad
05-06 15:59:44.911: I/DEBUG(3344): r0 deadbaad r1 00d9c060 r2 40000000 r3 00000000
05-06 15:59:44.911: I/DEBUG(3344): r4 00000000 r5 00000027 r6 415bf010 r7 00000062
05-06 15:59:44.911: I/DEBUG(3344): r8 415bf018 r9 00000047 10 100ffb94 fp 100ffbd8
05-06 15:59:44.911: I/DEBUG(3344): ip ffffffff sp 100ffb50 lr 40071121 pc 4006d880 cpsr 60000030
05-06 15:59:44.911: I/DEBUG(3344): d0 400000003eaaaaab d1 3ff000003f800000
05-06 15:59:44.911: I/DEBUG(3344): d2 457ff80000000fff d3 000000003f000000
05-06 15:59:44.911: I/DEBUG(3344): d4 00001fff00000000 d5 3fe999999999999a
05-06 15:59:44.911: I/DEBUG(3344): d6 3ff0000000000000 d7 3eaaaaab3f800000
05-06 15:59:44.911: I/DEBUG(3344): d8 0000000000000000 d9 0000000000000000
05-06 15:59:44.911: I/DEBUG(3344): d10 0000000000000000 d11 0000000000000000
05-06 15:59:44.911: I/DEBUG(3344): d12 0000000000000000 d13 0000000000000000
05-06 15:59:44.911: I/DEBUG(3344): d14 0000000000000000 d15 0000000000000000
05-06 15:59:44.911: I/DEBUG(3344): scr 80000012
05-06 15:59:45.011: I/DEBUG(3344): #00 pc 00017880 /system/lib/libc.so
05-06 15:59:45.011: I/DEBUG(3344): #01 pc 00007d8e /system/lib/libcutils.so (mspace_free)
05-06 15:59:45.011: I/DEBUG(3344): #02 pc 0007b746 /system/lib/libdvm.so (_Z21dvmHeapSourceFreeListjPPv)
05-06 15:59:45.011: I/DEBUG(3344): #03 pc 00042f88 /system/lib/libdvm.so
05-06 15:59:45.011: I/DEBUG(3344): #04 pc 00032fc8 /system/lib/libdvm.so (_Z22dvmHeapBitmapSweepWalkPK10HeapBitmapS1_jjPFvjPPvS2_ES2_)
05-06 15:59:45.011: I/DEBUG(3344): #05 pc 00042f44 /system/lib/libdvm.so (_Z27dvmHeapSweepUnmarkedObjectsbbPjS_)
05-06 15:59:45.011: I/DEBUG(3344): #06 pc 000336ac /system/lib/libdvm.so (_Z25dvmCollectGarbageInternalPK6GcSpec)
05-06 15:59:45.011: I/DEBUG(3344): #07 pc 0007bc1c /system/lib/libdvm.so
05-06 15:59:45.011: I/DEBUG(3344): #08 pc 0005f906 /system/lib/libdvm.so
05-06 15:59:45.011: I/DEBUG(3344): #09 pc 00012e04 /system/lib/libc.so (__thread_entry)
05-06 15:59:45.011: I/DEBUG(3344): #10 pc 00012958 /system/lib/libc.so (pthread_create)
05-06 15:59:45.011: I/DEBUG(3344): code around pc:
05-06 15:59:45.011: I/DEBUG(3344): 4006d860 4623b15c 2c006824 e026d1fb b12368db \.#F$h.,..&..h#.
05-06 15:59:45.011: I/DEBUG(3344): 4006d870 21014a17 6011447a 48124798 24002527 .J.!zD.`.G.H'%.$
05-06 15:59:45.011: I/DEBUG(3344): 4006d880 f7f47005 2106ee60 eeeef7f5 460aa901 .p..`..!.......F
05-06 15:59:45.011: I/DEBUG(3344): 4006d890 f04f2006 94015380 94029303 eab8f7f5 . O..S..........
05-06 15:59:45.011: I/DEBUG(3344): 4006d8a0 4622a905 f7f52002 f7f4eac2 2106ee4c .."F. ......L..!
05-06 15:59:45.011: I/DEBUG(3344): code around lr:
05-06 15:59:45.011: I/DEBUG(3344): 40071100 41f0e92d 46804c0c 447c2600 68a56824 -..A.L.F.&|D$h.h
05-06 15:59:45.011: I/DEBUG(3344): 40071110 e0076867 300cf9b5 dd022b00 47c04628 gh.....0.+..(F.G
05-06 15:59:45.011: I/DEBUG(3344): 40071120 35544306 37fff117 6824d5f4 d1ee2c00 .CT5...7..$h.,..
05-06 15:59:45.011: I/DEBUG(3344): 40071130 e8bd4630 bf0081f0 000283da 41f0e92d 0F..........-..A
05-06 15:59:45.011: I/DEBUG(3344): 40071140 fb01b086 9004f602 461f4815 4615460c .........H.F.F.F
05-06 15:59:45.011: I/DEBUG(3344): memory map around addr deadbaad:
05-06 15:59:45.011: I/DEBUG(3344): be97c000-be99d000 [stack]
05-06 15:59:45.011: I/DEBUG(3344): (no map for address)
05-06 15:59:45.011: I/DEBUG(3344): ffff0000-ffff1000 [vectors]
05-06 15:59:45.011: I/DEBUG(3344): stack:
05-06 15:59:45.011: I/DEBUG(3344): 100ffb10 4009965c /system/lib/libc.so
05-06 15:59:45.011: I/DEBUG(3344): 100ffb14 00d9c060 [heap]
05-06 15:59:45.011: I/DEBUG(3344): 100ffb18 00000a96
05-06 15:59:45.011: I/DEBUG(3344): 100ffb1c 4006fecd /system/lib/libc.so
05-06 15:59:45.011: I/DEBUG(3344): 100ffb20 4009970c /system/lib/libc.so
05-06 15:59:45.011: I/DEBUG(3344): 100ffb24 4009e85c
05-06 15:59:45.011: I/DEBUG(3344): 100ffb28 00000000
05-06 15:59:45.011: I/DEBUG(3344): 100ffb2c 40071121 /system/lib/libc.so
05-06 15:59:45.011: I/DEBUG(3344): 100ffb30 00000000
05-06 15:59:45.011: I/DEBUG(3344): 100ffb34 100ffb64
05-06 15:59:45.011: I/DEBUG(3344): 100ffb38 415bf010 /dev/ashmem/dalvik-heap (deleted)
05-06 15:59:45.011: I/DEBUG(3344): 100ffb3c 00000062
05-06 15:59:45.011: I/DEBUG(3344): 100ffb40 415bf018 /dev/ashmem/dalvik-heap (deleted)
05-06 15:59:45.011: I/DEBUG(3344): 100ffb44 4007028d /system/lib/libc.so
05-06 15:59:45.011: I/DEBUG(3344): 100ffb48 df0027ad
05-06 15:59:45.021: I/DEBUG(3344): 100ffb4c 00000000
05-06 15:59:45.021: I/DEBUG(3344): #00 100ffb50 00000000
05-06 15:59:45.021: I/DEBUG(3344): 100ffb54 00000000
05-06 15:59:45.021: I/DEBUG(3344): 100ffb58 00000000
05-06 15:59:45.021: I/DEBUG(3344): 100ffb5c 00000000
05-06 15:59:45.021: I/DEBUG(3344): 100ffb60 00cf2780 [heap]
05-06 15:59:45.021: I/DEBUG(3344): 100ffb64 fffffbdf
05-06 15:59:45.021: I/DEBUG(3344): 100ffb68 00000020
05-06 15:59:45.021: I/DEBUG(3344): 100ffb6c 00000020
05-06 15:59:45.021: I/DEBUG(3344): 100ffb70 00000000
05-06 15:59:45.021: I/DEBUG(3344): 100ffb74 40018d91 /system/lib/libcutils.so
05-06 15:59:45.021: I/DEBUG(3344): #01 100ffb78 00cf2780 [heap]
05-06 15:59:45.021: I/DEBUG(3344): 100ffb7c 4162fe00 /dev/ashmem/dalvik-heap (deleted)
05-06 15:59:45.021: I/DEBUG(3344): 100ffb80 100ffcf4
05-06 15:59:45.021: I/DEBUG(3344): 100ffb84 00000062
05-06 15:59:45.021: I/DEBUG(3344): 100ffb88 415bf018 /dev/ashmem/dalvik-heap (deleted)
05-06 15:59:45.021: I/DEBUG(3344): 100ffb8c 40800749 /system/lib/libdvm.so
05-06 15:59:45.661: I/BootReceiver(1265): Copying /data/tombstones/tombstone_01 to DropBox (SYSTEM_TOMBSTONE)
05-06 15:59:45.671: I/DEBUG(3344): debuggerd committing suicide to free the zombie!
05-06 15:59:45.671: I/DEBUG(3440): debuggerd: Apr 28 2013 11:10:17
05-06 15:59:45.681: D/Zygote(917): Process 3347 terminated by signal (11)
05-06 15:59:45.681: I/ActivityManager(1265): haveBgApp:true app.setAdj:10
05-06 15:59:45.681: I/ActivityManager(1265): Process com.nef.xxx (pid 3347) has died.
05-06 15:59:45.681: W/ActivityManager(1265): Scheduling restart of crashed service com.nef.xxx/.service.renderService in 5000ms
05-06 15:59:48.241: D/PowerManagerService(1265): Screen must keep ON all the time! TimeoutTask return.
05-06 15:59:50.691: D/dalvikvm(3441): Late-enabling CheckJNI
05-06 15:59:50.701: I/ActivityManager(1265): Start proc com.nef.xxx for service com.nef.xxx/.service.renderService: pid=3441 uid=10009 gids={1015, 3003}
05-06 15:59:50.721: I/dalvikvm(3441): Turning on JNI app bug workarounds for target SDK version 9...
這個錯誤並非再調用某個jni接口的時候發生的編程
而是反覆調用以後(或是上層進行了一些其餘操做後)冷不丁的蹦出來數組
程序雖然沒有彈框,但進程已經掛了安全
這種隨機問題最難搞了,很難肯定哪行代碼出的問題app
因而各類百度谷歌尋求解決方案ide
其中最重要的錯誤信息是 Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1)函數
網上也有不少人都遇到相似的問題測試
主要癥結仍是內存操做的問題ui
在通過各類排查測試後,折騰了老半天
終於找到問題所在,的確是內存操做有誤
在jni裏,我想把jbyteArray轉化成char*
因而寫了個轉化函數,原型以下:
問題就出在
這句話
假如GetByteArrayElements返回的是abc
則chars_len值爲3
而chars[3]=0就等因而數組越界訪問修改了
這樣無形當中就破壞了堆內存給程序留下安全隱患
到特定時候就會觸發錯誤爆發
後函數改成:
就沒有問題了
在調用函數到處理了char*以後再delete掉就ok了
哎,C++的指針真是讓人又愛又恨
之後你們遇到相似問題
仍是好好檢查下native代碼
看看有沒有指針操做不當的問題
指針有風險,操做需謹慎
僅以此文小記,但願對你們有幫助~