From a5bfb0d670e109fd1514ff117e1d598a068b05d5 Mon Sep 17 00:00:00 2001 From: localvar Date: Thu, 14 Nov 2019 08:46:53 +0000 Subject: [PATCH] update jni for memory leak detection --- src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h | 8 ++++++++ src/client/src/TSDBJNIConnector.c | 12 ++++++++++++ src/util/src/tmem.c | 5 +++++ 3 files changed, 25 insertions(+) diff --git a/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h b/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h index 9f9632cadc..d99fca5ce1 100644 --- a/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h +++ b/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h @@ -9,6 +9,14 @@ extern "C" { #endif #undef com_taosdata_jdbc_TSDBJNIConnector_INVALID_CONNECTION_POINTER_VALUE #define com_taosdata_jdbc_TSDBJNIConnector_INVALID_CONNECTION_POINTER_VALUE 0LL +/* + * Class: com_taosdata_jdbc_TSDBJNIConnector + * Method: + * Signature: (Ljava/lang/String;)V + */ +JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_detectMemoryLeakImp + (JNIEnv *, jclass, jstring); + /* * Class: com_taosdata_jdbc_TSDBJNIConnector * Method: initImp diff --git a/src/client/src/TSDBJNIConnector.c b/src/client/src/TSDBJNIConnector.c index d958679544..4123c2aa12 100644 --- a/src/client/src/TSDBJNIConnector.c +++ b/src/client/src/TSDBJNIConnector.c @@ -111,6 +111,18 @@ void jniGetGlobalMethod(JNIEnv *env) { jniTrace("native method register finished"); } +JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_detectMemoryLeakImp(JNIEnv *env, jobject jobj, jstring jPath) { + if (jPath != NULL) { + const char *path = (*env)->GetStringUTFChars(env, jPath, NULL); + taos_dump_memory_leak_at_exit(path); + (*env)->ReleaseStringUTFChars(env, jPath, path); + } else { + taos_dump_memory_leak_at_exit(NULL); + } + + jniGetGlobalMethod(env); +} + JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_initImp(JNIEnv *env, jobject jobj, jstring jconfigDir) { if (jconfigDir != NULL) { const char *confDir = (*env)->GetStringUTFChars(env, jconfigDir, NULL); diff --git a/src/util/src/tmem.c b/src/util/src/tmem.c index 3ca6cedf29..81dff55049 100644 --- a/src/util/src/tmem.c +++ b/src/util/src/tmem.c @@ -310,6 +310,11 @@ static void dump_memory_leak_at_sig(int sig) { } void taos_dump_memory_leak_at_exit(const char* path) { + if (fpMemLeak != NULL) { + printf("memory leak detection already enabled.\n"); + return; + } + if (path == NULL || path[0] == 0) { fpMemLeak = stdout; } else if ((fpMemLeak = fopen(path, "w")) == NULL) { -- GitLab