비트 맵을 처리하고 문자열을 반환하는 자바 메서드가 있습니다. JNI (VS 2010)에서이 메서드를 호출하면 작동하지만 많은 경우이 메서드를 호출하면 크래시가 발생할 때까지 프로세스 메모리가 증가합니다. 메모리를 많이 사용하는 명령은 다음과 같습니다JNI NewByteArray 메모리 누수
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, 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));
}
에
가 왜'_ReleaseByteArrayElements를 수행하여 선을()'의견을 않았다 변경입니까? – fge
동일하기 때문에 메모리가 올라가고 충돌이 발생합니다 ... – Riccardo
언제든지 'buff'를 해제하지 않는 것 같습니다. 'GetUnmanaged'는 절대로 회수되지 않는 임시 버퍼를 할당합니까? –