I want to allocate some memory in C and keep it associated with a java object instance, like this:
void configure(JNIEnv *object, jobject obj, ....) {
char *buf = new char[1024];
// associated <buf> with <obj> somehow
}
And then later free the memory when the java object gets garbage collected – I could do this by calling a JNI function from the finalize() method of the java object.
The question is, how do I associate a C pointer with the java object? Keep a long field in the object and cast the pointer to long? Is there a better way?
Generally, if you want to transfer a pointer from C to Java, it’s recommended to use
longso that there are enough bits to hold the pointer value in case the platform is 64 bits.Then, have a look at
ByteBuffer.allocateDirect()which creates aByteBufferinstance which memory can be shared with C. You can allocate such a directByteBufferfrom the Java side then pass it as ajobjectto a JNI function and inside this JNI function you use theGetDirectBufferAddressfunction.Another way is to wrap a native area of memory with the
NewDirectByteBufferJNI function from the native side. It gives you ajobjectyou pass back to the Java side (pay attention to local and global references). Pay attention to the fact that once the directByteBufferthat wraps the native memory has been created, you are still responsible for managing the native memory: at some point, you will have to calldelete buf;in your native code, Java won’t do it for you.