Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JNI how do I attach C/C++ data to Java Object ? (opaque memory chunk)

843829Aug 28 2005 — edited Aug 29 2005
I've looked over the JNI specification and compiled my first JNI demo program all is well from the examples.

However it really strikes me that for me to effectivly use JNI I need to attach one or more opaque pointers to my Java object that my JNI code can get back, modify or remove at a later date.

All of the example code I have seen is for a simplistic call into C/C++ and get immediate Java result passed back. I have not seen any provision to manage more persistant C/C++ data by attaching it to a Java object.

Once attached I would then need to confirm I get a callback to clean up maybe Object.release() is my friend here?


Q1 How do I attach some piece of data to my Java object, I say some piece as obviously I need to allocate 4 bytes on i386 and 8 bytes on ia64 which will then point to a C/C++ heap allocation from malloc (that in turn could be a C object graph).

Q2 Is it completely safe to use libc malloc() / free() from my JNI library, i.e. I don't have to do any memory allocation porting ? Nothing I've read says I need too, how does this interact with the JVMs own allocation stratagy for Java code ?

Q3 Can I hook into the release() call, the easiest way seems to override it in Java and have that call a private native method to check over the object and de-attach the data. Would this be a good approach ?


My platform Linux Sun JVM, 1.4.2/1.5.0
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 26 2005
Added on Aug 28 2005
3 comments
720 views