在多个jni调用中保持某种c ++对象的存活

编程入门 行业动态 更新时间:2024-10-24 12:23:00
本文介绍了在多个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.

有没有办法解决这个问题? BTW:我刚接触c ++。

Is there a way to handle this ? BTW: I am newly to c++.

推荐答案

我不确定我是否理解你的问题是正确的。但我想你想要做的是在多个jni调用上保持某种c ++对象的存活。

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:这将是如果你发布一些代码,会更有帮助。但是我希望这里的伪代码能有所帮助。

BTW: It would be much more helpful if you would post some code. But I hope this pseudo code here helps a little.

更多推荐

在多个jni调用中保持某种c ++对象的存活

本文发布于:2023-07-15 10:27:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110659.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   对象   jni

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!