Android Jni : crash in global and local ref variables

落花浮王杯 提交于 2019-12-06 04:24:34

The "other" thread must also be attached to use the JIOManager. Why not get a local reference also there? On the other hand, your crash dump suggests that actually you are only looking at 3 classes. So You can create the global references to these once (even in JNI_OnLoad() if you find it easier), and use them from all your native threads.

There is a limit on the number of local references per thread, and you're exceeding it. The most common way to do this is to create local references in a loop and not delete them as you go.

Local references are cleaned up automatically (and near-instantly) when execution returns to the VM, so for simple uses its best to just let the VM do the work. But if you're creating more than 16 local references, it's a good idea to do the clean up manually.

The call to FindClass creates a local reference. The call to NewGlobalRef creates a global reference, without destroying the local reference. You should modify your code to look like this:

jclass temp = CJavaEnv::getInstance()->env()->FindClass(ioManagerName);
jclass JIOManager = (jclass)CJavaEnv::getInstance()->env()->NewGlobalRef(temp);
CJavaEnv::getInstance()->env()->DeleteLocalRef(temp);

Global references are not destroyed unless you explicitly destroy them, so be careful about leaking them. When CheckJNI is enabled there's a cap of a few thousand; without CheckJNI they're allowed to accumulate without bound.

See also JNI Tips.

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