当我应该释放的原生(Android的NDK)处理? [英] When should I free the native (Android NDK) handles?

查看:170
本文介绍了当我应该释放的原生(Android的NDK)处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用本机库(这不是我的决定)。图书馆已经拥有了JNI包装器,而Android的例子NDK code ++工程。但库初始化子程序返回原始手柄和开发者需要正确地关闭它们

I have to use a native library (this is not my decision). The library already has a JNI wrapper, and the example Android NDK code works. But the library initialization routines return native handles and the developer is required to correctly close them.

现在,有一个有趣的问题:?从哪里调用close_handle程序

Now, there's an interesting question: where to call the close_handle routines from?

目前至少在理论上,每个不正确的终止可能导致磁盘或一些其他类型的资源泄漏的上某处左临时文件。

At least in theory, each incorrect termination may result in temporary files left somewhere on the disk or some other kind of resource leak.

库初始化需要0.5至1秒,消耗大量的内存。

Library initialization takes from 0.5 to 1 sec and consumes a lot of memory.

这是活动是一个控制器(MVC中的意义上),机器人能杀死它的,因为自身的原因,包括打开装置,这是保证的唯一功能被称为是的onPause()。因此,的onPause / onResume 是一个长期消耗资源的操作不好的地方。

An Activity is a controller (in MVC sense), Android can kill it because of its own reasons, including turning the device, and the only function that is guaranteed to be called is onPause(). So onPause/onResume is a bad place for a long resource-consuming operation.

(我知道关于的android:configChanges =keyboardHidden |方向,但我会preFER,这并不需要一个解决方案。)

(I know about android:configChanges="keyboardHidden|orientation", but I would prefer a solution that does not need it.)

应用将是一个理想的人选(我认为图书馆是模型的一部分​​),但是没有应用程序终止事件。

Application would be an ideal candidate (I consider the library to be a part of the model), but there is no "application terminates" event.

A 服务听起来有前景,本机库的的一个服务,但我不知道怎样才能有希望的行为:当应用程序终止手柄必须关闭。

A Service sounds promising, that native library is a service, but I just do not see how I can have the desired behavior: the handles must be closed when the application terminates.

超时:听起来像一个权衡,但实际上它保证了内存将无法使用需要它的时刻,但将成为可几秒钟后

Timeouts: sounds like a trade-off, but in fact it guarantees that the memory will not be available the moment it is needed but will become available a few seconds later.

推荐答案

在实践中,事实证明,一些本地库泄漏资源;我不得不做一个进程重启(这个没杀活动,但重新创建过程中):

In practice, it turned out that some native library leaks resources; I had to do a process restart (this did not kill the activity, but re-created the process):

<一个href=\"http://stackoverflow.com/questions/6609414/howto-programatically-restart-android-app/13173279#13173279\">howto编程&QUOT;重启&QUOT; Android应用?

人们只能希望,没有库泄漏临时磁盘文件...

One can only hope that no library leaks temporary disk files...

这篇关于当我应该释放的原生(Android的NDK)处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆