aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Asim Shankar <ashankar@google.com>2017-02-27 13:35:15 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2017-02-27 14:25:26 -0800
commit1092bfd98c1533a8f04cdc4178d1dea15137599e (patch)
tree098c0615d6b9749fc669f1141c1e3c3fa4f518f7
parent95a7c5e06d7c4a74041b8cc882386265b0a243ed (diff)
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
-rw-r--r--tensorflow/java/src/main/native/exception_jni.cc9
1 files 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 <stdarg.h>
#include <stdio.h>
+#include <stdlib.h>
#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<char*>(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);
}