我似乎对JNI没什么好运。我正在等一本我买的书到了,但现在它是试错了。
我正在使用JNI来实现Lua评估程序。 evaluateatorNew()只是创建一个新的Evaluator()对象,创建一个新的CPtr()类对象,如Java端所述,将Evaluator()对象的指针分配给CPtr的'peer'成员,并返回新的CPtr jobject。 / p>
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
有任何帮助吗?如果我需要澄清任何事情,请告诉我。
答案 0 :(得分:3)
您的代码很多都是理所当然的。您需要检查每个的结果,我的意思是每个 JNI调用,通过查找异常,并在您获得异常时中止。你不能只是像这样堆叠函数调用Lisp风格,并希望它们都返回可用的值。
答案 1 :(得分:1)
AllocObject
分配但不构造对象。我怀疑SetLongField
如果你在一个未构造的物体上召唤它可能会猛烈地呕吐,就像你在这里做的那样。请改用NewObject
;首先,你必须查询无参数构造函数的methodid。
答案 2 :(得分:0)
您应该始终使用CheckJNI:http://android-developers.blogspot.com/2011/07/debugging-android-jni-with-checkjni.html
它会告诉你,你正在调用一个异常挂起的JNI函数,并且干净地中止。