title: Extending my Java API to C++ through JNI: How to wrap a method returning a String?
body: <p>I am using JNI to extend my API in Java, in order to make it accessible through C++ (I am creating wrapper classes). I am experienced in Java but new to C++. </p>
  
  <p>Now, I am creating a wrapper in C++ for a Java function getString() which returns a String. I am using GetStringUTFChars to get the C string from the Java String but I don't know where to use ReleaseStringUTFChars which releases the C string. Here is my code (aClass and aClassInstance are private date members inside the wrapper class):</p>
  
  <pre><code>const char* getString() {
  jmethodID methodID = env-&gt;GetMethodID(aClass,
          "methodName",
          "()Ljava/lang/String;");
  if (methodID == NULL) {
      cout &lt;&lt; "--methodID = NULL";
      exit(0);
  }
  jstring jstringResult = (jstring) env-&gt;CallObjectMethod(aClassInstance, methodID);
  const char* result = env-&gt;GetStringUTFChars(jstringResult, NULL);
  // env-&gt;ReleaseStringUTFChars(jstringResult, result); 
  return result;
  }
  </code></pre>
  
  <p>If I remove the // and use ReleaseStringUTFChars, result will be released and I will no longer be able to return it, right? Should I copy result to another char array before releasing it?</p>
  
  <p>Thanks, any help is welcome!</p>
  
  <p>K</p>