From 9968c61fbe02ac76d4a49fe60faf2f20f3ccee11 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 22 Sep 2014 15:07:40 +0200 Subject: [PATCH] [#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 --- .../src/main/c/io_netty_channel_epoll_Native.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/transport-native-epoll/src/main/c/io_netty_channel_epoll_Native.c b/transport-native-epoll/src/main/c/io_netty_channel_epoll_Native.c index 03501c5a93..2a06c2267c 100644 --- a/transport-native-epoll/src/main/c/io_netty_channel_epoll_Native.c +++ b/transport-native-epoll/src/main/c/io_netty_channel_epoll_Native.c @@ -343,6 +343,8 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) { } jobject directBuffer = (*env)->NewDirectByteBuffer(env, mem, 1); if (directBuffer == NULL) { + free(mem); + throwOutOfMemoryError(env, "Error allocating native buffer"); 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. posId = (*env)->GetMethodID(env, cls, "position", "()I"); if (posId == NULL) { + free(mem); + // position method was not found.. something is wrong so bail out throwRuntimeException(env, "Unable to find method ByteBuffer.position()"); return JNI_ERR; @@ -360,12 +364,16 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) { limitId = (*env)->GetMethodID(env, cls, "limit", "()I"); if (limitId == NULL) { + free(mem); + // limit method was not found.. something is wrong so bail out throwRuntimeException(env, "Unable to find method ByteBuffer.limit()"); return JNI_ERR; } updatePosId = (*env)->GetMethodID(env, cls, "position", "(I)Ljava/nio/Buffer;"); if (updatePosId == NULL) { + free(mem); + // position method was not found.. something is wrong so bail out throwRuntimeException(env, "Unable to find method ByteBuffer.position(int)"); 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 (*env)->ExceptionClear(env); } + + free(mem); + jclass fileRegionCls = (*env)->FindClass(env, "io/netty/channel/DefaultFileRegion"); if (fileRegionCls == NULL) { // pending exception...