通过多个 jni 调用保持某种 c++ 对象处于活动状态
问题说明
我的 java 代码将调用现有的 c 代码来解析文件.它将生成一个保存许多数据的对象.我将调用jni的第二种方法来访问这些数据,当我调用第二种方法时,我必须再次解析文件.这显然是正确的行为.
My java code will call the exist c code to parse the file. it will generate an object which keep many data. I will call the jni's second method to access such data, When I call the second method, I have to parse the file again. it is obviously the right behavior.
有没有办法解决这个问题?顺便说一句:我是 C 新手.
Is there a way to handle this ? BTW: I am newly to c .
正确答案
我不确定我是否理解你的问题.但我猜你想要做的是让某种 c 对象在多个 jni 调用中保持活跃.
I am not sure if I understand your question correct. But I guess what you want to do is to keep some sort of c object alive over multiple jni calls.
你可以做很多事情.首先解析您的文件并将您的 c 对象存储在全局变量中.这是最简单的解决方案,但不是很好.
You can do multiple things. First parse your file and store your c object in a global variable. This is the simplest solution but not a nice one.
您还可以将 c 对象的生命周期移到 java 中.
You can also move the life cycle of your c object into java.
jlong java_some_class_jni_method(...)
{
.... parse your text file ....
MyParseclass* cls = new MyParseclass(...);
....
return (jlong) cls;
}
但请记住,您需要再次删除这个原生 c 类.所以你需要一个 jni 方法,并且一定要调用它.
But keep in mind that you need to delete this native c class again. So you need a jni method to this and be sure to call it.
void java_some_calls_jni_method(..., jlong clsPtr)
{
MyParseclass* cls = (MyParseclass*)clsPtr;
... do maybe do something with cls and access the data...
delete cls; // do not use the jlong again in any call
}
顺便说一句:如果您发布一些代码会更有帮助.但我希望这里的伪代码能有所帮助.
BTW: It would be much more helpful if you would post some code. But I hope this pseudo code here helps a little.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tangbfbhk
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20