Note that there are some explanatory texts on larger screens.

plurals
  1. POMemory leak using JNI to retrieve String's value from Java code
    text
    copied!<p>I'm using GetStringUTFChars to retrieve a string's value from the java code using JNI and releasing the string using ReleaseStringUTFChars. When the code is running on JRE 1.4 there is no memory leak but if the same code is running with a JRE 1.5 or higher version the memory increases. This is a part of the code </p> <pre><code>msg_id=(*env)-&gt;GetStringUTFChars(env, msgid,NULL); opcdata_set_str(opc_msg_id, OPCDATA_MSGID, msg_id); (*env)-&gt;ReleaseStringUTFChars(env, msgid,msg_id); </code></pre> <p>I'm unable to understand the reason for leak.Can someone help?</p> <hr> <p>This one is because if I comment the rest of the code but leave this part the memory leak takes place. This is a part of the code that I'm using</p> <pre><code>JNIEXPORT jobjectArray JNICALL Java_msiAPI_msiAPI_msgtoescalate( JNIEnv *env, jobject job, jstring msgid, jlong msgseverity, jstring msgprefixtext, jint flag ) { opcdata opc_msg_id; /* data struct to store a mesg ID */ const char *msg_id; int ret, ret2; jint val; val=67; jstring str=NULL; jobjectArray array = NULL; jclass sclass=NULL; /* create an opc_data structure to store message ids of */ /* messages to escalate */ if ((ret2=opcdata_create(OPCDTYPE_MESSAGE_ID, &amp;opc_msg_id))!= OPC_ERR_OK) { fprintf(stderr, "Can't create opc_data structure to store message. opcdata_create()=%d\n", ret2); cleanup_all(); } ////////////////////////////////////////////////////////// msg_id=(*env)-&gt;GetStringUTFChars(env,msgid,NULL); opcdata_set_str(opc_msg_id, OPCDATA_MSGID, msg_id); (*env)-&gt;ReleaseStringUTFChars(env, msgid, msg_id); ret=opcmsg_ack(connection,opc_msg_id); ////////////////////////////////////////////////////////// if(flag==0 &amp;&amp; ret==0) { sclass = (*env)-&gt;FindClass(env, "java/lang/String"); array = (*env)-&gt;NewObjectArray(env, 2, sclass, NULL); str=(*env)-&gt;NewStringUTF(env,"0"); (*env)-&gt;SetObjectArrayElement(env,array,0,str); (*env)-&gt;DeleteLocalRef(env, str); str=(*env)-&gt;NewStringUTF(env,"0"); (*env)-&gt;SetObjectArrayElement(env,array,1,str); (*env)-&gt;DeleteLocalRef(env, str); } opcdata_free(&amp;opc_msg_id); if(ret!=0) return NULL; else return(array); } </code></pre> <p>In the one above is if I comment the sections between ///// I don't see any memory leak.</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