看来我对 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
有什么帮助吗?如果我需要澄清任何事情,请告诉我。