JNI Local vs Global Reference: is not a valid JNI reference

和自甴很熟 提交于 2019-12-11 04:54:06

问题


I am seeing this warning in my JNI code:

JNI WARNING: 0x44be7258 is not a valid JNI reference

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

Header:

...
jclass m_class;

Cpp:

m_class = env->FindClass( classSignature );

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


回答1:


From the Liang book, chapter 5.1.1 "Local References"

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.

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.



来源:https://stackoverflow.com/questions/11907832/jni-local-vs-global-reference-is-not-a-valid-jni-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!