gpt4 book ai didi

java - JNI中如何释放返回的对象?

转载 作者:太空宇宙 更新时间:2023-11-04 12:37:51 24 4
gpt4 key购买 nike

<分区>

我的程序发生了内存泄漏。

请帮我找到内存泄漏的位置并告诉我如何修复,谢谢。

代码

extern "C" JNIEXPORT jbyteArray JNICALL
Java_cn_test_encoder_JNIEncoder_encodeEncryptBuff(
JNIEnv* env,
jobject obj,
jbyteArray ucInBuffer,
jintArray unInbufferLen,
jintArray unOutbufferLen) {

jbyte *bytes = env->GetByteArrayElements(ucInBuffer, 0);;

int chars_len = env->GetArrayLength(ucInBuffer);

unsigned char* chars = (unsigned char *) malloc(chars_len);
memset(chars,0,chars_len);
memcpy(chars, bytes, chars_len);
env->ReleaseByteArrayElements(ucInBuffer, bytes, 0);

int inLength = env->GetArrayLength(unInbufferLen);
int* inArray = env->GetIntArrayElements(unInbufferLen,0);

int outLength = env->GetArrayLength(unOutbufferLen);
int* outArray = env->GetIntArrayElements(unOutbufferLen,0);

unsigned char* encryptBuff = Encode_Encrypt_Buff(chars, (unsigned int*)inArray, (unsigned int*)outArray);

jbyteArray bArray=env->NewByteArray(*outArray);

env->SetByteArrayRegion(bArray, 0, *outArray, (jbyte *)encryptBuff);

env->ReleaseIntArrayElements(unInbufferLen, inArray, NULL);
env->ReleaseIntArrayElements(unOutbufferLen, outArray, NULL);
free(chars);

return bArray; // java needs byte array
}

内存使用情况

memory usage

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com