Note that there are some explanatory texts on larger screens.

plurals
  1. POJNI memory management using the Invocation API
    text
    copied!<p>When I'm building a java object using JNI methods, in order to pass it in as a parameter to a java method I'm invoking using the JNI invocation API, how do I manage its memory? </p> <p>Here's what I am working with:</p> <p>I have a C object that has a destructor method that is more complex that <code>free()</code>. This C object is to be associated with a Java object, and once the application is finished with the Java object, I have no more need for the C object.</p> <p>I am creating the Java object like so (error checking elided for clarity):</p> <pre><code>c_object = c_object_create (); class = (*env)-&gt;FindClass (env, "my.class.name"); constructor = (*env)-&gt;GetMethodID (env, class, "&lt;init&gt;", "(J)V"); instance = (*env)-&gt;NewObject (env, class, constructor, (jlong) c_object); method = (*env)-&gt;GetMethodID (env, other_class, "doSomeWork", "(Lmy.class.name)V"); (*env)-&gt;CallVoidMethod (env, other_class, method, instance); </code></pre> <p>So, now that I'm done with <code>instance</code>, what do I do with it? Ideally, I'd like to leave the garbage collection up to the VM; when it's done with <code>instance</code> it would be fantastic if it also called <code>c_object_destroy()</code> on the pointer I provided to it. Is this possible?</p> <p>A separate, but related question has to do with the scope of Java entities that I create in a method like this; do I have to manually release, say, <code>class</code>, <code>constructor</code>, or <code>method</code> above? The JNI doc is frustratingly vague (in my judgement) on the subject of proper memory management.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload