How to keep a C++ object inside Java code? Possible?

偶尔善良 提交于 2020-01-13 02:44:26

问题


First I've got a legacy C++ library, and I'm migrating my app to Android with new UI.

During runtime, there might be multiple C++ objects created and destroyed dynamically. I built a set of JNI native methods to call into library to get that. But where should I keep those C++ objects? I have to keep track of them and free them if they're unnecessary.

So far I only knew Java can't reference to a C++ object. Is it possible to make a Java wrapper class which include a reference to C++ object?

Thanks.


回答1:


Yes, it's possible. You can store a pointer to your C++ object in a simple java int. That's exactly how SWIG does that.




回答2:


JavaCPP takes care of this detail by saving it into a wrapping peer Pointer object.



来源:https://stackoverflow.com/questions/5577559/how-to-keep-a-c-object-inside-java-code-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!