开发者

Smart pointer and external code interaction via JNI

开发者 https://www.devze.com 2023-02-25 03:12 出处:网络
I\'m wondering what the best way is to have a pointer (via memory address) to an object in a C++ library which makes liberal use of smart pointers (ns-3). At the moment, I use path-based lookup and I\

I'm wondering what the best way is to have a pointer (via memory address) to an object in a C++ library which makes liberal use of smart pointers (ns-3). At the moment, I use path-based lookup and I'm concerned about its efficiency, since it seems to do full tree traversal for each call. If I mainta开发者_C百科in a standard pointer to the object for use across the JNI boundary, then the object may be disposed of while Java still holds a reference to it. Is the best idea to use a pointer to a smart pointer instead?

Cheers,

Chris


That was basically our solution. On the Java side, objects are held as simple ids, with a very simple algorithmic mapping of the id to a pointer (a reinterpret_cast, in fact). For objects managed by reference counted pointers, the "object" pointed to by the id is a reference counted pointer.

And just in case: don't forget to free it in the finalizer.

0

精彩评论

暂无评论...
验证码 换一张
取 消