0

看来我对 JNI 一点运气都没有。我正在等待我买的书到货,但现在它是试错法。

我正在使用 JNI 来实现 Lua 评估器。evaluatorNew() 只是创建一个新的 Evaluator() 对象,创建一个新的 CPtr() 类对象,如 Java 端所述,将 Evaluator() 对象的指针分配给 CPtr 的“对等”成员,并返回新的 CPtr 对象。

JNIEXPORT jobject JNICALL Java_com_starlon_libscriptable_UtilsEvaluator_evaluatorNew(
    JNIEnv *env, jclass clazz)
{
    int foo = 1;
    while(foo);

    Evaluator *eval = new Evaluator();

    jobject obj;
    jclass tempClass;

    tempClass = env->FindClass("com/starlon/libscriptable/CPtr");

    obj = env->AllocObject( tempClass );
    if (obj)
    {
        env->SetLongField( obj, env->GetFieldID( tempClass, "peer", "J" ), (jlong)eval);
    }
    return obj;
}

这是保存指针的 Java 类。这就是 'env->FindClass()' 调用的内容。

public class CPtr
{
    /* Pointer value of the real C pointer. Use long to be 64-bit safe. */
    private long peer;

    protected long getPeer()
    {
        return peer;
    }

    /* No-args constructor. */
    CPtr() {}

}

如下所示,没有堆栈跟踪可言。

(gdb) bt
#0  0xafd15ca8 in __libc_android_abort ()
   from /home/scott/workspace/StarVisuals/StarVisuals/obj/local/armeabi-v7a/libc.so
#1  0x00000008 in ?? ()
Backtrace stopped: frame did not save the PC

这是 gdb 逐步执行上述 JNI 函数的转储:

0x80805258 in Java_com_starlon_libscriptable_UtilsEvaluator_evaluatorNew (env=0xabd8, 
    clazz=0x40715698) at jni/libscriptable//evaluator.cpp:85
85      while(foo);
Current language:  auto; currently c++
(gdb) set foo = 0
(gdb) s
[New Thread 11226]
[Switching to Thread 11226]
87      Evaluator *eval = new Evaluator();
(gdb) u
92      tempClass = env->FindClass("com/starlon/libscriptable/CPtr");
(gdb) u
94      obj = env->AllocObject( tempClass );
(gdb) u
95      if (obj)
(gdb) s
97          env->SetLongField( obj, env->GetFieldID( tempClass, "peer", "J" ), (jlong)eval);
(gdb) s
_JNIEnv::GetFieldID (this=0xabd8, clazz=0x40715910, name=0x8080a134 "peer", sig=0x8080a13c "J")
    at /opt/android-ndk-r7b/platforms/android-9/arch-arm/usr/include/jni.h:708
708     { return functions->GetFieldID(this, clazz, name, sig); }
(gdb) print functions
$1 = (const JNINativeInterface *) 0xaca9dd68
(gdb) print this
$2 = (_JNIEnv * const) 0xabd8
(gdb) print clazz
$3 = (jclass) 0x40715910
(gdb) print name
$4 = 0x8080a134 "peer"
(gdb) print sig
$5 = 0x8080a13c "J"
(gdb) print *functions
$6 = {reserved0 = 0x0, reserved1 = 0x0, reserved2 = 0x0, reserved3 = 0x0, GetVersion = 0xaca43385, 
  DefineClass = 0xaca43355, FindClass = 0xaca4773d, FromReflectedMethod = 0xaca47719, 
  FromReflectedField = 0xaca476f5, ToReflectedMethod = 0xaca476b9, GetSuperclass = 0xaca44801, 
  IsAssignableFrom = 0xaca4450d, ToReflectedField = 0xaca4767d, Throw = 0xaca43331, 
  ThrowNew = 0xaca47655, ExceptionOccurred = 0xaca447b1, ExceptionDescribe = 0xaca4761d, 
  ExceptionClear = 0xaca43315, FatalError = 0xaca44d99, PushLocalFrame = 0xaca475c1, 
  PopLocalFrame = 0xaca47561, NewGlobalRef = 0xaca44ac1, DeleteGlobalRef = 0xaca4502d, 
---Type <return> to continue, or q <return> to quit---q
SetByteFieldQuit
(gdb) print functions->GetFieldID
$7 = (jfieldID (*)(JNIEnv *, jclass, const char *, const char *)) 0xaca47435
(gdb) s

Program received signal SIGSEGV, Segmentation fault.
0xaca4cd0c in ?? ()
(gdb) bt
#0  0xafd15ca8 in __libc_android_abort ()
   from /home/scott/workspace/StarVisuals/StarVisuals/obj/local/armeabi-v7a/libc.so
#1  0x00000008 in ?? ()
Backtrace stopped: frame did not save the PC
(gdb) quit

这是 jni.h 的第 708 行:

jfieldID GetFieldID(jclass clazz, const char* name, const char* sig)
{ return functions->GetFieldID(this, clazz, name, sig); } // 708

有什么帮助吗?如果我需要澄清任何事情,请告诉我。

4

3 回答 3

3

您的代码需要很多东西是理所当然的。您需要通过查找异常并在遇到异常时中止来检查每个 JNI 调用的结果,我的意思是每个JNI 调用。您不能只是像 Lisp 风格那样堆叠函数调用,并希望它们都返回可用的值。

于 2012-04-25T08:59:48.073 回答
1

AllocObject分配但不构造对象。我怀疑SetLongField如果你在一个未构造的物体上调用它可能会猛烈呕吐,就像你在这里做的那样。改为使用NewObject;首先,您必须查询无参数构造函数的methodid。

于 2012-04-25T02:26:21.860 回答
0

您应该始终使用 CheckJNI 运行:http ://android-developers.blogspot.com/2011/07/debugging-android-jni-with-checkjni.html

它会告诉您,您正在调用一个带有异常挂起的 JNI 函数,并且干净地中止了。

于 2012-04-30T14:15:01.017 回答