java - Plain C++ objects in code accessed by JNI -
my android application has native c++ layer , java layer. java layer accesses native layer via jni calls.
can c++ layer safely create c++ objects own internal use, , store them in c++ member variables? i'm talking c++ objects don't need accessed java code in way, i.e. managed , deleted in c++ layer. in other words, same object (via member variable) accessed throughout multiple jni calls, c++ layer needs access it.
i need confirmation in matter, because know there special jni methods handling objects (relevant terms: local reference, global reference, etc.). if i'm correct, these apply objects visible (or created for) java code well.
therefore, suppose native-only c++ objects can created , deleted in usual ways (such new
, delete
), , java doesn't need know them. no special interoperability considerations necessary, long objects , references them reside exclusively in c++ layer. correct? can define c++ classes , methods in layer in same way if usual c++ application without jni/java interoperability? general, allowed instantiate , store plain c++ objects, i.e. objects out of authority of dalvik/jvm?
indeed, jni methods handling objects the java objects.
you can create c/c++ objects in way can imagine (malloc/new), but: how preserve them across jni calls? (if need that, of course.) 2 options:
convert pointer integer , pass integer java. (you have care pointers stored in garbage-collected java objects, see, integers not imply freeing native memory.)
have c/c++ data structure necessary references.
thread-safety deserves separate consideration; if try store pointers in thread-local variables, never debug code (you see, there's life cycle mismatch).
good results may achieved static global variables accessed 1 thread.
and keep in mind android may kill , restart application's process, destroying stored in static variables, native data structures.
Comments
Post a Comment