20

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

msg_id=(*env)->GetStringUTFChars(env, msgid,NULL); 
opcdata_set_str(opc_msg_id, OPCDATA_MSGID, msg_id);
(*env)->ReleaseStringUTFChars(env, msgid,msg_id); 

I'm unable to understand the reason for leak.Can someone help?


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

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, &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)->GetStringUTFChars(env,msgid,NULL);
  opcdata_set_str(opc_msg_id, OPCDATA_MSGID, msg_id);
  (*env)->ReleaseStringUTFChars(env, msgid, msg_id);
  ret=opcmsg_ack(connection,opc_msg_id);
  //////////////////////////////////////////////////////////

  if(flag==0 && ret==0)
  {
    sclass = (*env)->FindClass(env, "java/lang/String");
    array = (*env)->NewObjectArray(env, 2, sclass, NULL);
    str=(*env)->NewStringUTF(env,"0");
    (*env)->SetObjectArrayElement(env,array,0,str);
    (*env)->DeleteLocalRef(env, str);
    str=(*env)->NewStringUTF(env,"0");
    (*env)->SetObjectArrayElement(env,array,1,str);
    (*env)->DeleteLocalRef(env, str);
  }

  opcdata_free(&opc_msg_id);

  if(ret!=0)
    return NULL;
  else
    return(array);
}

In the one above is if I comment the sections between ///// I don't see any memory leak.

user207421
  • 305,947
  • 44
  • 307
  • 483
rperez
  • 8,430
  • 11
  • 36
  • 44
  • 5
    Seems ok, how do you know this is the cause of your leak? – Mike Tunnicliffe May 27 '09 at 14:08
  • 5
    I agree with fd - please post some information on how you know this is the leak. Do you have profiler data indicating this? – Lawrence Dol May 27 '09 at 18:05
  • 7
    It might be that the released string is not instantly garbage collected. Therefore, you observe increase in memory usage. You could also try to comment out just the opcdata_set_str() or opcmsg_ack() call and check if it still leaks memory. – akarnokd Jun 12 '09 at 13:45
  • 3
    What is the prototype (declaration) for the opcdata_set_str function please ? – Eric Nicolas Sep 10 '14 at 12:29
  • 2
    ... and what does it do? I would say the leak is inside that function. Is there some complementary action you're supposed to take to free that data item? – user207421 Jun 06 '15 at 23:42
  • 1
    What are you using to measure memory usage? Are you leaking Java heap or native heap? – Gili Jul 04 '15 at 04:21

1 Answers1

1

Release array object.

(*env)->DeleteLocalRef(env, array);

Cheung
  • 174
  • 5