Reputation: 1466
With JNI, I'm creating a weak global ref from a jobject. And later, when trying to access this reference, the dalvik vm crashes on Android 2.3 but not on 4.1 and 4.2. What I found out was that IsSameObject with the object itself and the created weak reference as parameters returns 1 on the working devices and 0 on the devices where it crashes. Does that make sense? Changing to GlobalRef and IsSameObject returns 1.
Here I'm testing IsSameObject right after creating the weak global reference.
this->jMyObject = getEnv()->NewWeakGlobalRef(jMyObject);
LOGV("Is same object = %d", getEnv()->IsSameObject(jMyObject, this->jMyObject));
LOGV is my macro for printing to logcat.
The crash log isn't very helpful to me:
01-09 09:57:04.778: I/DEBUG(13012): Build fingerprint: 'google/passion/passion:2.3.7/GWK74/121341:user/release-keys'
01-09 09:57:04.778: I/DEBUG(13012): pid: 15001, tid: 15001 >>> org.example.myproject <<<
01-09 09:57:04.778: I/DEBUG(13012): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr de5d8e6f
01-09 09:57:04.778: I/DEBUG(13012): r0 0018fed8 r1 00000007 r2 de5d8e6f r3 00000002
01-09 09:57:04.778: I/DEBUG(13012): r4 437d45d4 r5 431ecda8 r6 beb54098 r7 000010f8
01-09 09:57:04.778: I/DEBUG(13012): r8 aca12000 r9 0000000c 10 431ecd94 fp aca9f368
01-09 09:57:04.778: I/DEBUG(13012): ip 000000f8 sp beb54048 lr aca15e2c pc aca15e18 cpsr a0000010
01-09 09:57:04.778: I/DEBUG(13012): d0 0065007200670067 d1 006e006900730000
01-09 09:57:04.778: I/DEBUG(13012): d2 275449202121213a d3 4546455220412000
01-09 09:57:04.778: I/DEBUG(13012): d4 8000000000000000 d5 429800003f800000
01-09 09:57:04.778: I/DEBUG(13012): d6 00000000c2980000 d7 4110000042500000
01-09 09:57:04.778: I/DEBUG(13012): d8 0000000043eb0000 d9 c35c0000439ce2e0
01-09 09:57:04.778: I/DEBUG(13012): d10 433d5140405277a8 d11 0000000042354500
01-09 09:57:04.778: I/DEBUG(13012): d12 0000000000000000 d13 0000000000000000
01-09 09:57:04.778: I/DEBUG(13012): d14 0000000000000000 d15 0000000000000000
01-09 09:57:04.778: I/DEBUG(13012): d16 000000000065dea0 d17 c053000000000000
01-09 09:57:04.778: I/DEBUG(13012): d18 0000000000000000 d19 0000000000000000
01-09 09:57:04.778: I/DEBUG(13012): d20 3ff0000000000000 d21 8000000000000000
01-09 09:57:04.778: I/DEBUG(13012): d22 0000000000000000 d23 0000000000000000
01-09 09:57:04.778: I/DEBUG(13012): d24 0000000000000000 d25 407c000000000000
01-09 09:57:04.778: I/DEBUG(13012): d26 4030000000000000 d27 3ff0000000000000
01-09 09:57:04.778: I/DEBUG(13012): d28 0100010001000100 d29 3ff0000000000000
01-09 09:57:04.778: I/DEBUG(13012): d30 0000000000000000 d31 3ff0000000000000
01-09 09:57:04.778: I/DEBUG(13012): scr 20000010
01-09 09:57:04.828: I/DEBUG(13012): #00 pc 00015e18 /system/lib/libdvm.so
01-09 09:57:04.828: I/DEBUG(13012): #01 pc 0001c0e4 /system/lib/libdvm.so
01-09 09:57:04.828: I/DEBUG(13012): #02 pc 0001afdc /system/lib/libdvm.so
01-09 09:57:04.828: I/DEBUG(13012): #03 pc 00059c40 /system/lib/libdvm.so
01-09 09:57:04.828: I/DEBUG(13012): #04 pc 00046666 /system/lib/libdvm.so
01-09 09:57:04.828: I/DEBUG(13012): #05 pc 001b0d3e /data/data/org.example/lib/libmyproject.so
01-09 09:57:04.828: I/DEBUG(13012): #06 pc 001b4c7e /data/data/org.example/lib/libmyproject.so
Do I need to extract the object from the reference? Or should I just avoid weak references altogether?
Upvotes: 1
Views: 797
Reputation: 1466
If I put the weak reference in a NewLocalRef it seems to be doing fine. So I ended up with:
jobject jMyObject = getEnv()->NewLocalRef(jMyWeakObject);
if(!getEnv()->IsSameObject(jMyObject, NULL)) {
// do stuff with object
}
And the member variable is now jMyWeakObject.
Upvotes: 4