java - Avoiding memory leaks in jni code -


i'm writing jni code in have convert std::string jstring , vice versa, i'm using following functions

//std::string jstring  const char *cons_ref = any_std_string.c_str();  jstring jref = env->newstringutf(cons_ref);  //jstring std::string  const char *cons_ref = env->getstringutfchars(any_jstring, 0);  std::string any_std_string = cons_ref 

but leading creation of lot of const char* read , cannot deleted, causing memory leaks.

is there better technique these conversions avoid memory leaks. in advance.

whenever you're dealing returns pointer, should @ documentation function. ideally should tell whether or not you're responsible deallocating/releasing memory. , if should tell how.

i don't know jni, little googling found http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html

on page says:

const char * getstringutfchars(jnienv *env, jstring string, jboolean *iscopy);

returns pointer array of bytes representing string in modified utf-8 encoding. array valid until released releasestringutfchars().

so sounds should doing like:

//jstring std::string  const char *cons_ref = env->getstringutfchars(any_jstring, 0);  std::string any_std_string = cons_ref;  env->releasestringutfchars(any_jstring, cons_ref); 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -