JNI本地参考与全局参考:不是有效的JNI参考 [英] JNI Local vs Global Reference: is not a valid JNI reference

查看:81
本文介绍了JNI本地参考与全局参考:不是有效的JNI参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JNI代码中看到此警告:

I am seeing this warning in my JNI code:

JNI WARNING: 0x44be7258 is not a valid JNI reference

我正在将JNI中FindClass方法返回的LocalReference分配给构造函数中的类成员变量:

I am assigning a LocalReference returned by FindClass method in JNI to my class member variable within the constructor:

标题:

...
jclass m_class;

Cpp:

m_class = env->FindClass( classSignature );

FindClass是否返回LocalReference并将其存储在我的类成员变量中无效?

Does FindClass return a LocalReference and storing it in my class member variable is invalid?

推荐答案

来自梁氏书,第5.1.1章本地参考"

From the Liang book, chapter 5.1.1 "Local References"

大多数JNI函数都会创建本地引用...本地引用仅在创建它的本机方法的动态上下文内有效,并且仅在该本机方法的一次调用内有效.一旦本机方法返回,将释放在本机方法执行期间创建的所有本地引用.

Most JNI functions create local references ... A local reference is valid only within the dynamic context of the native method that creates it, and only within that one invocation of the native method. All local references created during the execution of a native method will be freed once the native method returns.

其后是一个完全使用您的方法FindClass的非法代码示例.换句话说,是的,FindClass返回本地引用.在下一章中,是一个创建全局引用的示例,该全局引用可以按您想要的方式使用.当您不再需要DeleteGlobalRef时,请不要忘记它.否则JVM无法对其进行GC,您的程序将泄漏.

Followed by an illegal code example which uses exactly your method FindClass. In other words, yes, FindClass returns a local reference. In the following chapter is an example of creating a global reference which is usable in the way you wanted. Don't forget to DeleteGlobalRef when you don't need it anymore. Otherwise JVM cannot GC it and your program will leak.

这篇关于JNI本地参考与全局参考:不是有效的JNI参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆