使用JNI内存泄漏检索从Java code字符串的值 [英] Memory leak using JNI to retrieve String's value from Java code

查看:168
本文介绍了使用JNI内存泄漏检索从Java code字符串的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可利用GetStringUTFChars检索使用JNI和释放使用ReleaseStringUTFChars字符串在java code字符串的值。当code为JRE 1.4上运行,没有内存泄漏,但如果在同一code与JRE 1.5或更高版本的运行内存增加。这是code的一部分

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); 

我无法理解leak.Can的原因有人帮忙吗?

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

这一个是因为如果我注释掉code的休息,但保留这部分内存泄漏发生。这是code的一部分,我使用

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.

推荐答案

发布的阵列的对象。

(* ENV) - > DeleteLocalRef(ENV,数组);

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

这篇关于使用JNI内存泄漏检索从Java code字符串的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆