我遇到了 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
orNewGlobalRef
, 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/