ビットマップを処理して文字列を返すJavaメソッドがあります。 私はこのメソッドを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'は決して回収されない一時バッファを割り当てていますか? –