[#2926] Fix 1 byte memory leak in native transport

Motivation:

We use malloc(1) in the on JNI_OnLoad method but never free the allocated memory. This means we have a tiny memory leak of 1 byte.

Modifications:

Call free(...) on previous allocated memory.

Result:

Fix memory leak
This commit is contained in:
Norman Maurer 2014-09-22 15:07:40 +02:00
parent cb85ed9d66
commit 9968c61fbe

View File

@ -343,6 +343,8 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) {
} }
jobject directBuffer = (*env)->NewDirectByteBuffer(env, mem, 1); jobject directBuffer = (*env)->NewDirectByteBuffer(env, mem, 1);
if (directBuffer == NULL) { if (directBuffer == NULL) {
free(mem);
throwOutOfMemoryError(env, "Error allocating native buffer"); throwOutOfMemoryError(env, "Error allocating native buffer");
return JNI_ERR; return JNI_ERR;
} }
@ -353,6 +355,8 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) {
// it is not possible to obtain the position and limit using the fields directly. // it is not possible to obtain the position and limit using the fields directly.
posId = (*env)->GetMethodID(env, cls, "position", "()I"); posId = (*env)->GetMethodID(env, cls, "position", "()I");
if (posId == NULL) { if (posId == NULL) {
free(mem);
// position method was not found.. something is wrong so bail out // position method was not found.. something is wrong so bail out
throwRuntimeException(env, "Unable to find method ByteBuffer.position()"); throwRuntimeException(env, "Unable to find method ByteBuffer.position()");
return JNI_ERR; return JNI_ERR;
@ -360,12 +364,16 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) {
limitId = (*env)->GetMethodID(env, cls, "limit", "()I"); limitId = (*env)->GetMethodID(env, cls, "limit", "()I");
if (limitId == NULL) { if (limitId == NULL) {
free(mem);
// limit method was not found.. something is wrong so bail out // limit method was not found.. something is wrong so bail out
throwRuntimeException(env, "Unable to find method ByteBuffer.limit()"); throwRuntimeException(env, "Unable to find method ByteBuffer.limit()");
return JNI_ERR; return JNI_ERR;
} }
updatePosId = (*env)->GetMethodID(env, cls, "position", "(I)Ljava/nio/Buffer;"); updatePosId = (*env)->GetMethodID(env, cls, "position", "(I)Ljava/nio/Buffer;");
if (updatePosId == NULL) { if (updatePosId == NULL) {
free(mem);
// position method was not found.. something is wrong so bail out // position method was not found.. something is wrong so bail out
throwRuntimeException(env, "Unable to find method ByteBuffer.position(int)"); throwRuntimeException(env, "Unable to find method ByteBuffer.position(int)");
return JNI_ERR; return JNI_ERR;
@ -383,6 +391,9 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) {
// this is ok as we can still use the method so just clear the exception // this is ok as we can still use the method so just clear the exception
(*env)->ExceptionClear(env); (*env)->ExceptionClear(env);
} }
free(mem);
jclass fileRegionCls = (*env)->FindClass(env, "io/netty/channel/DefaultFileRegion"); jclass fileRegionCls = (*env)->FindClass(env, "io/netty/channel/DefaultFileRegion");
if (fileRegionCls == NULL) { if (fileRegionCls == NULL) {
// pending exception... // pending exception...