JNI Local vs Global Reference: is not a valid JNI

2019-08-04 05:02发布

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条回答
Juvenile、少年°
2楼-- · 2019-08-04 05:11

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.

查看更多
登录 后发表回答