android - 来自 WeakGlobalRef 的 JNI LocalRef

标签 android reference java-native-interface jnienv

我遇到了 WeakGlobalRef 指向垃圾收集对象的问题,当我尝试在此引用上调用 CallVoidMethod 时,这个应用程序崩溃了。为了解决这个问题,我执行以下操作:

jobject javaObject = pEnv->NewLocalRef(m_pJavaObject);

if (javaObject)
{
    pEnv->CallVoidMethod(javaObject, method, object);
}

这仍然在 CallVoidMethod 崩溃,这样做是否有效,或者我是否也应该检查本地引用 IsSameObject

我得到的最后一件事,没有任何东西可以指向任何方法:

Crash reason:  SIGSEGV
Crash address: 0xdead4321
Process uptime: not available

Thread 0 (crashed)
(...)
41  libart.so + 0xba7df
     sp = 0xbeed2dc0    pc = 0xb4eca7e1
    Found by: stack scanning
42  libxxx.so!_JNIEnv::CallVoidMethod(_jobject*, _jmethodID*, ...) [jni.h : 650 + 0x1]
     sp = 0xbeed2dd8    pc = 0x9fabbeb9
    Found by: stack scanning

最佳答案

should I also do check on local ref IsSameObject

The documentation提供答案:

it is recommended that a standard (strong) local or global reference to the same object be acquired using the JNI functions NewLocalRef or NewGlobalRef, and that this strong reference be used to access the intended object. These functions will return NULL if the object has been freed, and otherwise will return a strong reference (which will prevent the object from being freed). The new reference should be explicitly deleted when immediate access to the object is no longer required, allowing the object to be freed.

如果对象已被释放,您将从 NewLocalRef 获得 NULL,因此无需调用 IsSameObject

关于android - 来自 WeakGlobalRef 的 JNI LocalRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40019971/

相关文章:

java - 通过 GCM 向设备发送消息时无效注册

pointers - Swift 使用 UnsafePointer<String> 从 UnsafeMutablePointer<Void> 获取值

python - 如何将对象交给 python 垃圾回收?

c++ - Rust 中指针和引用的区别

android - 为平台 2.1 构建 Android-openssl 库

java - Android 和 JNI,将数据传输到 FFmpeg

java - 在调试期间显示项目,否则不在 ListView 中显示项目

java - Firebase不会触发数据更改事件

Java NIO FileChannel - 从 Android TUN 网络接口(interface)读取空值

安卓工作室 :JNI DETECTED ERROR IN APPLICATION: native code passing in reference to invalid local reference: 0x200001