gpt4 book ai didi

java - 一般使用 java JNI GetFieldID 和 JNI

转载 作者:行者123 更新时间:2023-11-27 23:24:34 27 4
gpt4 key购买 nike

看来我对 JNI 的运气并不好。我正在等待我买的书到货,但现在是试错法。

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

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

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

最佳答案

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

关于java - 一般使用 java JNI GetFieldID 和 JNI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10308512/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com