"jstring points to non-string object" in JNI call

250 views
Skip to first unread message

Benn

unread,
Jul 3, 2010, 3:52:22 AM7/3/10
to android-ndk
Hi guys -

Vital Stats: Emulator: 2.1-update1, NDK: v4, API: 7

Trying to do what I thought would be a very simple test: pass a String
object from Java to a C JNI function and return the length. Just a
proof of concept, really:

...
public native static long testString(String s);

public void callTestString()
{
java.lang.String s = new java.lang.String("Hello World.");
long len = Lua.testString(s);
Log.e("test", "The length is: " + len);
}
...

and in C:

long Java_com_example_test_testString(JNIEnv *env, jstring test)
{
return (*env)->GetStringLength(env, test);
}

When I call this, I get this in LogCat:
WARN/dalvikvm(4542): JNI WARNING: jstring 0x44c19f10 points to non-
string object (Check_GetStringUTFChars)

And an exception, and a SEGV at 0xdeadd00d (included below to minimize
verbosity).

Any suggestions where I'm going astray here? I admit to excluding
some surrounding code, but there isn't much. The function is
definitely getting called (if I place a debug log statement above the
GetStringLength call, it gets printed). Everything I see online seems
to indicate I'm doing it correctly.

Cheers,
--B

07-03 07:39:16.447: WARN/dalvikvm(4798): JNI WARNING: jstring
0x44e9fe20 points to non-string object (Check_GetStringLength)
07-03 07:39:16.447: INFO/dalvikvm(4798): "Instr:
android.test.InstrumentationTestRunner" prio=5 tid=15 NATIVE
07-03 07:39:16.447: INFO/dalvikvm(4798): | group="main" sCount=0
dsCount=0 s=N obj=0x44eb4588 self=0x136e98
07-03 07:39:16.447: INFO/dalvikvm(4798): | sysTid=4804 nice=-8
sched=0/0 cgrp=default handle=1223888
07-03 07:39:16.447: INFO/dalvikvm(4798): at
com.example.test.testString(Native Method)
07-03 07:39:16.447: INFO/dalvikvm(4798): at
com.example.test.TestLuaInterface.testLoadMixin(TestLuaInterface.java:
36)
07-03 07:39:16.447: INFO/dalvikvm(4798): at
java.lang.reflect.Method.invokeNative(Native Method)
07-03 07:39:16.457: INFO/dalvikvm(4798): at
java.lang.reflect.Method.invoke(Method.java:521)
07-03 07:39:16.457: INFO/dalvikvm(4798): at
junit.framework.TestCase.runTest(TestCase.java:154)
07-03 07:39:16.457: INFO/dalvikvm(4798): at
junit.framework.TestCase.runBare(TestCase.java:127)
07-03 07:39:16.457: INFO/dalvikvm(4798): at
junit.framework.TestResult$1.protect(TestResult.java:106)
07-03 07:39:16.457: INFO/dalvikvm(4798): at
junit.framework.TestResult.runProtected(TestResult.java:124)
07-03 07:39:16.457: INFO/dalvikvm(4798): at
junit.framework.TestResult.run(TestResult.java:109)
07-03 07:39:16.457: INFO/dalvikvm(4798): at
junit.framework.TestCase.run(TestCase.java:118)
07-03 07:39:16.457: INFO/dalvikvm(4798): at
android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
07-03 07:39:16.457: INFO/dalvikvm(4798): at
android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
07-03 07:39:16.457: INFO/dalvikvm(4798): at
android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:
430)
07-03 07:39:16.457: INFO/dalvikvm(4798): at
android.app.Instrumentation
$InstrumentationThread.run(Instrumentation.java:1447)
07-03 07:39:16.457: ERROR/dalvikvm(4798): VM aborting
07-03 07:39:16.567: INFO/DEBUG(28): *** *** *** *** *** *** *** ***
*** *** *** *** *** *** *** ***
07-03 07:39:16.567: INFO/DEBUG(28): Build fingerprint: 'generic/sdk/
generic/:2.1-update1/ECLAIR/35983:eng/test-keys'
07-03 07:39:16.567: INFO/DEBUG(28): pid: 4798, tid: 4804 >>>
com.example.test <<<
07-03 07:39:16.567: INFO/DEBUG(28): signal 11 (SIGSEGV), fault addr
deadd00d
07-03 07:39:16.567: INFO/DEBUG(28): r0 00000354 r1 afe1331d r2
0000000c r3 deadd00d
07-03 07:39:16.567: INFO/DEBUG(28): r4 00000026 r5 44e9fe20 r6
ad065f64 r7 00089af0
07-03 07:39:16.567: INFO/DEBUG(28): r8 469c6b18 r9 42f12d8c 10
00000354 fp 42f12d8c
07-03 07:39:16.567: INFO/DEBUG(28): ip ad080160 sp 469c6ac0 lr
afe142dd pc ad035452 cpsr 20000030
07-03 07:39:16.657: INFO/DEBUG(28): #00 pc 00035452 /system/
lib/libdvm.so
07-03 07:39:16.657: INFO/DEBUG(28): #01 pc 00027a98 /system/
lib/libdvm.so
07-03 07:39:16.657: INFO/DEBUG(28): #02 pc 0002a662 /system/
lib/libdvm.so
07-03 07:39:16.667: INFO/DEBUG(28): #03 pc 0002a89a /system/
lib/libdvm.so
07-03 07:39:16.667: INFO/DEBUG(28): #04 pc 00002d32 /data/
data/com.example.text/lib/libtest.so
07-03 07:39:16.667: INFO/DEBUG(28): #05 pc 0000f1f4 /system/
lib/libdvm.so
07-03 07:39:16.677: INFO/DEBUG(28): #06 pc 00038018 /system/
lib/libdvm.so
07-03 07:39:16.677: INFO/DEBUG(28): #07 pc 000316a2 /system/
lib/libdvm.so
07-03 07:39:16.677: INFO/DEBUG(28): #08 pc 0003d31c /system/
lib/libdvm.so
07-03 07:39:16.687: INFO/DEBUG(28): #09 pc 0001fd2c /system/
lib/libdvm.so
07-03 07:39:16.687: INFO/DEBUG(28): #10 pc 00018d98 /system/
lib/libdvm.so
07-03 07:39:16.687: INFO/DEBUG(28): #11 pc 0004d3bc /system/
lib/libdvm.so
07-03 07:39:16.697: INFO/DEBUG(28): #12 pc 00054e74 /system/
lib/libdvm.so
07-03 07:39:16.697: INFO/DEBUG(28): #13 pc 0001fd2c /system/
lib/libdvm.so
07-03 07:39:16.697: INFO/DEBUG(28): #14 pc 00018d98 /system/
lib/libdvm.so
07-03 07:39:16.697: INFO/DEBUG(28): #15 pc 0004d6d0 /system/
lib/libdvm.so
07-03 07:39:16.707: INFO/DEBUG(28): #16 pc 0004d702 /system/
lib/libdvm.so
07-03 07:39:16.707: INFO/DEBUG(28): #17 pc 00041c78 /system/
lib/libdvm.so
07-03 07:39:16.707: INFO/DEBUG(28): #18 pc 00010000 /system/
lib/libc.so
07-03 07:39:16.717: INFO/DEBUG(28): #19 pc 0000fad4 /system/
lib/libc.so
07-03 07:39:16.717: INFO/DEBUG(28): code around pc:
07-03 07:39:16.717: INFO/DEBUG(28): ad035440 4808ecb6 6b9b5823
d0002b00 4b064798
07-03 07:39:16.717: INFO/DEBUG(28): ad035450 701c2426 ed28f7d9
0004ab1c fffe57c4
07-03 07:39:16.717: INFO/DEBUG(28): ad035460 fffe8c30 00000354
deadd00d b510b40e
07-03 07:39:16.717: INFO/DEBUG(28): code around lr:
07-03 07:39:16.717: INFO/DEBUG(28): afe142cc 220ce008 2b005eab
1c28d003 47889901
07-03 07:39:16.726: INFO/DEBUG(28): afe142dc 35544306 d5f43f01
2c006824 b003d1ee
07-03 07:39:16.726: INFO/DEBUG(28): afe142ec bdf01c30 00024b44
000000b4 1c0fb5f0
07-03 07:39:16.726: INFO/DEBUG(28): stack:
07-03 07:39:16.726: INFO/DEBUG(28): 469c6a80 00000015
07-03 07:39:16.726: INFO/DEBUG(28): 469c6a84 afe1334d /system/
lib/libc.so
07-03 07:39:16.726: INFO/DEBUG(28): 469c6a88 afe3902c /system/
lib/libc.so
07-03 07:39:16.737: INFO/DEBUG(28): 469c6a8c afe38fd8 /system/
lib/libc.so
07-03 07:39:16.737: INFO/DEBUG(28): 469c6a90 00000000
07-03 07:39:16.737: INFO/DEBUG(28): 469c6a94 afe142dd /system/
lib/libc.so
07-03 07:39:16.737: INFO/DEBUG(28): 469c6a98 00136e98 [heap]
07-03 07:39:16.737: INFO/DEBUG(28): 469c6a9c afe1331d /system/
lib/libc.so
07-03 07:39:16.737: INFO/DEBUG(28): 469c6aa0 00089af0 [heap]
07-03 07:39:16.737: INFO/DEBUG(28): 469c6aa4 ad07ff50 /system/
lib/libdvm.so
07-03 07:39:16.747: INFO/DEBUG(28): 469c6aa8 44e9fe20 /dev/
ashmem/mspace/dalvik-heap/2 (deleted)
07-03 07:39:16.747: INFO/DEBUG(28): 469c6aac ad065f64 /system/
lib/libdvm.so
07-03 07:39:16.747: INFO/DEBUG(28): 469c6ab0 00089af0 [heap]
07-03 07:39:16.747: INFO/DEBUG(28): 469c6ab4 afe1337f /system/
lib/libc.so
07-03 07:39:16.747: INFO/DEBUG(28): 469c6ab8 df002777
07-03 07:39:16.747: INFO/DEBUG(28): 469c6abc e3a070ad
07-03 07:39:16.747: INFO/DEBUG(28): #00 469c6ac0 00000001
07-03 07:39:16.747: INFO/DEBUG(28): 469c6ac4 ad027a9d /system/
lib/libdvm.so
07-03 07:39:16.757: INFO/DEBUG(28): #01 469c6ac8 00000001
07-03 07:39:16.757: INFO/DEBUG(28): 469c6acc ad02a667 /system/
lib/libdvm.so

David Turner

unread,
Jul 4, 2010, 1:58:24 PM7/4/10
to andro...@googlegroups.com
A java long is not the same than a C long.

> --
> You received this message because you are subscribed to the Google Groups "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
>
>

Tim Mensch

unread,
Jul 4, 2010, 3:22:05 PM7/4/10
to andro...@googlegroups.com
Benn wrote:
> long Java_com_example_test_testString(JNIEnv *env, jstring test)
> {
> return (*env)->GetStringLength(env, test);
> }
>
I think your signature is wrong, and should read:

JNIEXPORT jlong JNICALL Java_com_example_test_testString (JNIEnv *,
jclass, jstring);

The jclass is the 'this' of the class the JNI function belongs to.
That's why it's saying your object isn't a string; it's not.

If you use javah to create your JNI method signatures, you'll not make
this mistake. I wrote a simple shell script to regenerate my JNI header
for me:

(cd bin && /c/Devel/jdk/bin/javah.exe -o
../jni/include/qz/android/jni_init.h -jni com.recharge.lib.JNIclass)

...where jni_init.h is the file it writes, and JNIclass is the name of
the Java class that is exporting the native functions. You can add
additional copies of that line for each header you want to generate.

If the signature changes, I get a build error in C++. I just
copy-and-paste from the header to the C++ file, adding in names for the
parameters.

Tim


__________ Information from ESET NOD32 Antivirus, version of virus signature database 5251 (20100704) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


Benn

unread,
Jul 4, 2010, 4:53:16 PM7/4/10
to android-ndk
In between the time this post was send and the moderation approved it,
I discovered much the same thing as the above. The important
difference was the missing jclass parameter, and the difference
between jlong (64-bit) and long (32-bit) return values.

Thanks, everyone!

Cheers,
--B
Reply all
Reply to author
Forward
0 new messages