From 1092bfd98c1533a8f04cdc4178d1dea15137599e Mon Sep 17 00:00:00 2001 From: Asim Shankar Date: Mon, 27 Feb 2017 13:35:15 -0800 Subject: Java: Memory leak fix and Windows friendliness. - Delete memory allocated for exception error messages. - Use vsnprintf instead of vasprintf for allocating exception error messages since the latter doesn't seem to be easily available on Windows. Change: 148687623 --- tensorflow/java/src/main/native/exception_jni.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tensorflow/java/src/main/native/exception_jni.cc b/tensorflow/java/src/main/native/exception_jni.cc index 2df0973389..4f9a84aa9a 100644 --- a/tensorflow/java/src/main/native/exception_jni.cc +++ b/tensorflow/java/src/main/native/exception_jni.cc @@ -15,6 +15,7 @@ limitations under the License. #include #include +#include #include "tensorflow/c/c_api.h" #include "tensorflow/java/src/main/native/exception_jni.h" @@ -29,12 +30,16 @@ const char kUnsupportedOperationException[] = void throwException(JNIEnv* env, const char* clazz, const char* fmt, ...) { va_list args; va_start(args, fmt); - char* message = nullptr; - if (vasprintf(&message, fmt, args) >= 0) { + // Using vsnprintf() instead of vasprintf() because the latter doesn't seem to + // be easily available on Windows. + const size_t max_msg_len = 512; + char* message = static_cast(malloc(max_msg_len)); + if (vsnprintf(message, max_msg_len, fmt, args) >= 0) { env->ThrowNew(env->FindClass(clazz), message); } else { env->ThrowNew(env->FindClass(clazz), ""); } + free(message); va_end(args); } -- cgit v1.2.3