kellogs
kellogs

Reputation: 2867

problems with JNI on android - Java instance methods

Hoping there are more knowledgeable people around, here I go. The attempts to call a java instance method from C almost succeed, but on a veery frail ground...

Problem #1 - Calling into a java method such as this one:

public void publishResult(String fromV8)
{
    try
    {
        //final String backupBuffer = "tututu";
        final String backupBuffer = String.valueOf(fromV8);
        runOnUiThread(new Runnable() {

            @Override
            public void run() 
            {
                ((TextView)findViewById(46)).setText(backupBuffer);
            }
        });
    } catch (Exception ex) 
    {
        ex.printStackTrace();
    }
}

works, but as soon as I try to do anything with the 'fromV8' parameter the JVM terminates with this kind of error message in LogCat:

04-26 00:16:10.714: INFO/DEBUG(14398): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
04-26 00:16:10.714: INFO/DEBUG(14398): Build fingerprint: 'LGE/thunderg/thunderg/thunderg:2.2/FRF91/eng.swapan.pati.20101012.153426:user/release-keys'
04-26 00:16:10.714: INFO/DEBUG(14398): >>> com.example.hellojni <<<
04-26 00:16:10.714: INFO/DEBUG(14398): signal 11 (SIGSEGV), fault addr 6c6c65bc
04-26 00:16:10.714: INFO/DEBUG(14398):  r0 00000001  r1 00000001  r2 42f8bf90  r3 0022e1a8

           [......plenty of registries and stack hexdump.....stripped]

04-26 00:16:10.814: INFO/DEBUG(14398):     46b5ed04  aca508fb  /system/lib/libdvm.so
04-26 00:16:11.124: INFO/ActivityManager(1363): Process com.example.hellojni (pid 16632) has died.
04-26 00:16:11.134: INFO/WindowManager(1363): WIN DEATH: Window{452be390 com.example.hellojni/com.example.hellojni.HelloJni paused=false}
04-26 00:16:11.134: INFO/WindowManager(1363): Setting rotation to 0, animFlags=1
04-26 00:16:11.144: INFO/BootReceiver(1363): Copying /data/tombstones/tombstone_09 to DropBox (SYSTEM_TOMBSTONE)
04-26 00:16:11.154: INFO/ActivityManager(1363): Config changed: { scale=1.0 imsi=226/5 

loc=en_GB touch=3 keys=1/1/2 nav=1/1 orien=1 layout=18 uiMode=17 seq=65}

And problem #2 - even if I do not do anything with the parameter but set a breakpoint inside the java method the JVM aborts again. Results are again in LogCat and it resembles the one above but has some extra two lines:

04-26 00:26:29.821: WARN/dalvikvm(16864): JDWP: slot 4 expected to hold object, 0x22e7b0 invalid
04-26 00:26:29.821: ERROR/dalvikvm(16864): VM aborting
[...+ SAME AS ABOVE...]

At no times have there shown up any exception traces in Console. Are these common symptoms or what ?

Thank you!

Upvotes: 0

Views: 1281

Answers (2)

SweetLou42
SweetLou42

Reputation: 21

I've seen that problem before when I got lazy on object parameters passed FROM c/c++ TO Java (for me the bugbear seems to be Strings).

env->CallStaticVoidMethod(interfaceClass, sendaString2Java, "Send me to Java");

Compiles just fine but throws a runtime error:

11-26 13:08:54.770: W/dalvikvm(1818): JDWP: slot 1 expected to hold object, 0xbea7a38c invalid

If you make the object you want to pass back a "Java Object" before returning it - all is well

env->CallStaticVoidMethod(interfaceClass, sendaString2Java, env->NewStringUTF("Send me to Java"));

works just fine.

I really lean hard on the example here: http://android.wooyd.org/JNIExample/ but never really figured out why the cpp file is in the assets folder instead of a JNI folder.

Upvotes: 2

CSchulz
CSchulz

Reputation: 11040

the SIGSEGV at fault addr 6c6c65bc indicates an invalid memory reference or segmentation fault (SIGSEGV).
For exceptions from the native code, you must explicit call on C/C++ side the method ExceptionDescribe(). More details you can find here Exceptions.

Upvotes: 0

Related Questions