Smart pointer and external code interaction via JNI
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,
ChrisThat 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.
精彩评论