c++ - How to free memory allocated by native method on Java side? -
i trying pass byte array (with random data) native java , not sure if causes memory leak or not. here's c++ code.
jniexport jbytearray jnicall java_com_sample_test_jni_testjni_return_1byte_1array (jnienv *env, jobject obj) { unsigned char *bytearray = new unsigned char[length]; srand(12345); for(int = 0;i < length;i++) { bytearray[i] = rand() % 64; } jbytearray data = (env)->newbytearray(length); env->setbytearrayregion(data, 0, length, (jbyte*)bytearray); delete(bytearray); return data; } and here's java code.
class testjni { static { system.loadlibrary("foobar"); } public native byte[] return_byte_array(); public static void main(string[] args) { byte[] data = new testjni().return_byte_array(); system.out.println("data length " + data.length); } } my doubt whether jbytearray allocated in native code garbage-collected java. cannot free in native side.
also, there docs describe jni memory management examples?
the java gc should clean objects allocate.
see this answer more details.
Comments
Post a Comment