Android Jni:全局和局部ref变量崩溃

时间:2014-09-21 11:46:23

标签: android multithreading android-ndk java-native-interface jnienv

我有调用java对象方法的jni c ++代码,例如:

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

    ..... some code

CJavaEnv::getInstance()->env()->DeleteLocalRef(JIOManager);

这在主线程中执行时非常好,但是当我尝试在另一个线程中执行它时 它崩溃,因为它说它是在另一个线程中创建的:O !!但是在我阅读谷歌android开发者网站后,他们说从任何线程访问Global Ref都没问题,所以我改变了那个代码。

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

    ..... some code

CJavaEnv::getInstance()->env()->DeleteGlobalRef(JIOManager);

但是我遇到了这个崩溃:

09-21 15:38:13.259: E/dalvikvm(3210): JNI ERROR (app bug): local reference table overflow (max=512)
09-21 15:38:13.259: W/dalvikvm(3210): JNI local reference table (0x76fb50) dump:
09-21 15:38:13.259: W/dalvikvm(3210):   Last 10 entries (of 512):
09-21 15:38:13.259: W/dalvikvm(3210):       511: 0x40a609a8 java.lang.Class<libcore.icu.NativeConverter>
09-21 15:38:13.259: W/dalvikvm(3210):       510: 0x42349610 byte[] (1 elements)
09-21 15:38:13.259: W/dalvikvm(3210):       509: 0x41d866b8 java.lang.Class<com.Cocoa.CocoaAndroidSDK.CocoaUnicodeConverter>
09-21 15:38:13.259: W/dalvikvm(3210):       508: 0x423495f8 byte[] (2 elements)
09-21 15:38:13.259: W/dalvikvm(3210):       507: 0x42349428 byte[] (1 elements)
09-21 15:38:13.259: W/dalvikvm(3210):       506: 0x41d866b8 java.lang.Class<com.Cocoa.CocoaAndroidSDK.CocoaUnicodeConverter>
09-21 15:38:13.259: W/dalvikvm(3210):       505: 0x42349410 byte[] (2 elements)
09-21 15:38:13.259: W/dalvikvm(3210):       504: 0x422b3f08 byte[] (1 elements)
09-21 15:38:13.259: W/dalvikvm(3210):       503: 0x41d866b8 java.lang.Class<com.Cocoa.CocoaAndroidSDK.CocoaUnicodeConverter>
09-21 15:38:13.259: W/dalvikvm(3210):       502: 0x422b3ef0 byte[] (2 elements)
09-21 15:38:13.259: W/dalvikvm(3210):   Summary:
09-21 15:38:13.259: W/dalvikvm(3210):       172 of java.lang.Class (3 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         1 of java.lang.String
09-21 15:38:13.259: W/dalvikvm(3210):         8 of byte[] (8 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):        20 of byte[] (1 elements) (20 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):        27 of byte[] (2 elements) (27 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         2 of byte[] (3 elements) (2 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):        16 of byte[] (4 elements) (16 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):        40 of byte[] (5 elements) (40 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         2 of byte[] (6 elements) (2 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         4 of byte[] (7 elements) (4 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):        22 of byte[] (8 elements) (22 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):        15 of byte[] (9 elements) (15 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):        40 of byte[] (10 elements) (40 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         3 of byte[] (11 elements) (3 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):        17 of byte[] (12 elements) (17 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         7 of byte[] (14 elements) (7 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         8 of byte[] (15 elements) (8 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):        15 of byte[] (16 elements) (15 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         8 of byte[] (17 elements) (8 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):        16 of byte[] (18 elements) (16 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         3 of byte[] (21 elements) (3 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         3 of byte[] (22 elements) (3 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):        17 of byte[] (24 elements) (17 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         4 of byte[] (28 elements) (4 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         8 of byte[] (30 elements) (8 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         1 of byte[] (32 elements)
09-21 15:38:13.259: W/dalvikvm(3210):         4 of byte[] (33 elements) (4 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         8 of byte[] (34 elements) (8 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         5 of byte[] (36 elements) (5 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         2 of byte[] (41 elements) (2 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         3 of byte[] (42 elements) (3 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         1 of byte[] (56 elements)
09-21 15:38:13.259: W/dalvikvm(3210):         4 of byte[] (66 elements) (4 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         4 of byte[] (72 elements) (4 unique instances)
09-21 15:38:13.259: W/dalvikvm(3210):         2 of byte[] (82 elements) (2 unique instances)
09-21 15:38:13.259: E/dalvikvm(3210): Failed adding to JNI local ref table (has 512 entries)
09-21 15:38:13.259: I/dalvikvm(3210): "AsyncTask #2" prio=5 tid=16 RUNNABLE
09-21 15:38:13.259: I/dalvikvm(3210):   | group="main" sCount=0 dsCount=0 obj=0x41decfd8 self=0x776200
09-21 15:38:13.259: I/dalvikvm(3210):   | sysTid=3236 nice=10 sched=0/0 cgrp=bg_non_interactive handle=7541280
09-21 15:38:13.259: I/dalvikvm(3210):   | schedstat=( 0 0 0 ) utm=82 stm=70 core=1
09-21 15:38:13.259: I/dalvikvm(3210):   at libcore.icu.NativeConverter.openConverter(Native Method)
09-21 15:38:13.259: I/dalvikvm(3210):   at java.nio.charset.CharsetEncoderICU.newInstance(CharsetEncoderICU.java:71)
09-21 15:38:13.259: I/dalvikvm(3210):   at java.nio.charset.CharsetICU.newEncoder(CharsetICU.java:27)
09-21 15:38:13.259: I/dalvikvm(3210):   at java.nio.charset.Charset.encode(Charset.java:451)
09-21 15:38:13.259: I/dalvikvm(3210):   at java.lang.String.getBytes(String.java:870)
09-21 15:38:13.259: I/dalvikvm(3210):   at com.Cocoa.CocoaAndroidSDK.CocoaUnicodeConverter.UTF8_TO_UTF16(CocoaUnicodeConverter.java:16)
09-21 15:38:13.259: I/dalvikvm(3210):   at com.Cocoa.CocoaAndroidSDK.CocoaSDKNativeLib.load(Native Method)
09-21 15:38:13.259: I/dalvikvm(3210):   at com.Cocoa.CocoaViewer.MainActivity$LongOperation.doInBackground(MainActivity.java:1498)
09-21 15:38:13.259: I/dalvikvm(3210):   at com.Cocoa.CocoaViewer.MainActivity$LongOperation.doInBackground(MainActivity.java:1)
09-21 15:38:13.259: I/dalvikvm(3210):   at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-21 15:38:13.259: I/dalvikvm(3210):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-21 15:38:13.259: I/dalvikvm(3210):   at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-21 15:38:13.259: I/dalvikvm(3210):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-21 15:38:13.259: I/dalvikvm(3210):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-21 15:38:13.259: I/dalvikvm(3210):   at java.lang.Thread.run(Thread.java:856)
09-21 15:38:13.259: E/dalvikvm(3210): VM aborting
09-21 15:38:13.259: A/libc(3210): Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1)

感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

每个线程的本地引用数量有限制,而且超出了它。最常见的方法是在循环中创建本地引用,而不是在删除时删除它们。

当执行返回到VM时,本地引用会自动(并且几乎立即)清除,因此为了简单使用,最好让VM完成工作。但是如果你创建了超过16个本地引用,那么手动清理是个好主意。

FindClass的调用会创建本地参考。对NewGlobalRef的调用会创建一个全局引用,而不会破坏本地引用。您应该修改您的代码,如下所示:

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

除非您明确销毁它们,否则不会销毁全局引用,因此请小心泄漏它们。启用CheckJNI时,上限为几千;没有CheckJNI,他们可以不受约束地累积。

另见JNI Tips

答案 1 :(得分:2)

&#34;其他&#34;还必须附加线程才能使用JIOManager。为什么不在那里获得本地参考?另一方面,您的崩溃转储表明实际上您只查看了3个类。所以你可以创建一次这些引用的全局引用(如果你发现它更容易在JNI_OnLoad()中),并在所有本机线程中使用它们。