java – JNI NewByteArray内存泄漏
发布时间:2020-12-14 05:24:37 所属栏目:Java 来源:网络整理
导读:我有一个 Java方法来处理位图并返回一个String. 当我从JNI(VS 2010)中调用此方法时,它可以工作,但是如果我多次调用该方法, 这个过程的记忆长大直到崩溃. 使用大量内存的指令是: jbyteArray jBuff = _env-NewByteArray(b-Length); 我的代码: static jobject
我有一个
Java方法来处理位图并返回一个String.
当我从JNI(VS 2010)中调用此方法时,它可以工作,但是如果我多次调用该方法, 这个过程的记忆长大直到崩溃. 使用大量内存的指令是: jbyteArray jBuff = _env->NewByteArray(b->Length); 我的代码: static jobject staticArray=0; System::String^ MyClass::ExecuteJavaMethod(System::Drawing::Bitmap^ bmp) { JNIEnv *_env; System::String^ out; unsigned const char * buff; int res = jvm->AttachCurrentThread((void **)&_env,NULL); if (jvm->GetEnv((void**) &_env,JNI_VERSION_1_6) != JNI_OK) { return "GetEnv ERROR"; } //save the bitmap in the stream MemoryStream^ ms = gcnew MemoryStream(); bmp->Save(ms,ImageFormat::Bmp); //get the bitmap buffer array<unsigned char>^b = ms->GetBuffer() ; //unmanaged conversion buff = GetUnmanaged(b,b->Length); //fill the buffer jbyteArray jBuff = _env->NewByteArray(b->Length); _env->SetByteArrayRegion(jBuff,b->Length,(jbyte*) buff); //call the java method jstring str = (jstring) _env->CallStaticObjectMethod ( Main,javaMethod,jBuff); // _env->ReleaseByteArrayElements(jBuff,(jbyte*)buff),0); //NOT WORKING //staticArray= _env->NewGlobalRef(jBuff); NOT //_env->DeleteLocalRef(jBuff); WORKING //return the string result of the java method return gcnew String(env->GetStringUTFChars(str,0)); } 解决方法
答案是:_env-> DeleteLocalRef(jBuff);
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |