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!

Clean up of objects created using NewObject()

843829Jun 5 2008 — edited Jun 6 2008
Hello
I've just started playing around with JNI in order to create a Java API layer on top of an existing C++ library. I figured out how to create an instance of a Java class in native code using env->NewObject() method. My question is who takes care of cleaning up this object? Is it my responsibility ?
jclass cl = env->FindClass("MyJavaClass");
jmethodID ctorId = env->GetMethodID(cl, "<init>", "()V");
jobject obj = env->NewObject(cl, ctorId);

// set member field (int)
jfieldID fid = env->GetFieldID(cl, "i", "I");
env->SetIntField(obj, fid, 5);

// set member field (string)
fid = env->GetFieldID(cl, "buffer", "Ljava/lang/String;");
jstring jstr = env->NewStringUTF("hello from cpp");
env->SetObjectField(obj, fid, jstr);

// return object back to Java
return obj;
Thanks
Amit
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 4 2008
Added on Jun 5 2008
2 comments
546 views