JNI层AttachCurrentThread与DetachCurrentThread

A JNI interface pointer (JNIEnv*) is passed as an argument for each native function mapped to a Java method, allowing for interaction with the JNI environment within the native method.This JNI interface pointer can be stored, but remains valid only in the current thread. Other threads must first call AttachCurrentThread()to attach themselves to the VM and obtain a JNI interface pointer. Once attached, a native thread works like a regular Java thread running within a native method. The native thread remains attached to the VM until it callsDetachCurrentThread()to detach itself.

最近在调试Android视频播放的时候,一旦退出当前视频打开另外一个视频,大约10秒后程序崩溃。最后定位到接收视频数据的线程,对视频数据进行解码渲染的时候会用到JVM

AttachCurrentThread函数附加JNI环境,而在退出当前视频播放时没有使用JVMDetachCurrentThread函数,那么当前线程无法退出导致JNI环境一直被占用。


  说明:在Android JNI层使用JVM时,如果在JNI层创建了线程并且使用了AttachCurrentThread函数,线程结束时一定要使用DetachCurrentThread函数。而JAVA层回调JNI层函数的线程则不必如此。

你可能感兴趣的:(Android)