Tôi có một phương thức Java xử lý bitmap và trả về một chuỗi. Khi tôi gọi phương pháp này từ JNI (VS 2010) nó hoạt động, nhưng nếu tôi gọi phương thức này nhiều lần, bộ nhớ của quá trình được tăng lên cho đến khi gặp sự cố. Các hướng dẫn sử dụng rất nhiều bộ nhớ là:Lỗi bộ nhớ JNI NewByteArray
jbyteArray jBuff = _env->NewByteArray(b->Length);
Mã của tôi:
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, 0, 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));
}
Tại sao bạn nhận xét dòng của bạn mà không '_ReleaseByteArrayElements() '? – fge
bởi vì là như nhau, bộ nhớ đi lên và treo ... – Riccardo
Bạn dường như không phát hành 'buff' tại bất kỳ thời điểm nào. Là 'GetUnmanaged' phân bổ một bộ đệm tạm thời mà không bao giờ được khai hoang? –