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层函数的线程则不必如此。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值