8186825: some memory leak issues in the transport_startTransport
authoramenkov
Wed, 18 Sep 2019 12:13:56 -0700
changeset 58220 2c4185d7276a
parent 58219 bc0648405d67
child 58221 b73753eff8b7
8186825: some memory leak issues in the transport_startTransport Reviewed-by: sspitsyn, phh
src/jdk.jdwp.agent/share/native/libjdwp/transport.c
--- a/src/jdk.jdwp.agent/share/native/libjdwp/transport.c	Wed Sep 18 20:56:20 2019 +0200
+++ b/src/jdk.jdwp.agent/share/native/libjdwp/transport.c	Wed Sep 18 12:13:56 2019 -0700
@@ -49,6 +49,15 @@
 
 static struct jdwpTransportCallback callback = {jvmtiAllocate, jvmtiDeallocate};
 
+static void freeTransportInfo(TransportInfo *info) {
+    if (info) {
+        jvmtiDeallocate(info->name);
+        jvmtiDeallocate(info->address);
+        jvmtiDeallocate(info->allowed_peers);
+        jvmtiDeallocate(info);
+    }
+}
+
 /*
  * Print the last transport error
  */
@@ -345,12 +354,14 @@
 
     LOG_MISC(("Begin accept thread"));
 
-    info = (TransportInfo*)(void*)arg;
+    info = (TransportInfo*)arg;
     t = info->transport;
     rc = (*t)->Accept(t, info->timeout, 0);
 
     /* System property no longer needed */
     setTransportProperty(jni_env, NULL);
+    /* TransportInfo data no longer needed */
+    freeTransportInfo(info);
 
     if (rc != JDWPTRANSPORT_ERROR_NONE) {
         /*
@@ -371,10 +382,14 @@
 static void JNICALL
 attachThread(jvmtiEnv* jvmti_env, JNIEnv* jni_env, void* arg)
 {
-    TransportInfo *info = (TransportInfo*)(void*)arg;
+    TransportInfo *info = (TransportInfo*)arg;
+    jdwpTransportEnv *t = info->transport;
+
+    /* TransportInfo data no longer needed */
+    freeTransportInfo(info);
 
     LOG_MISC(("Begin attach thread"));
-    connectionInitiated(info->transport);
+    connectionInitiated(t);
     LOG_MISC(("End attach thread"));
 }
 
@@ -484,7 +499,7 @@
     if (info->transport == NULL) {
         serror = loadTransport(name, info);
         if (serror != JDWP_ERROR(NONE)) {
-            jvmtiDeallocate(info);
+            freeTransportInfo(info);
             return serror;
         }
     }
@@ -577,6 +592,9 @@
             goto handleError;
         }
 
+        /* reset info - it will be deallocated by acceptThread */
+        info = NULL;
+
         launchCommand = debugInit_launchOnInit();
         if (launchCommand != NULL) {
             serror = launch(launchCommand, name, retAddress);
@@ -592,10 +610,7 @@
         return JDWP_ERROR(NONE);
 
 handleError:
-        jvmtiDeallocate(info->name);
-        jvmtiDeallocate(info->address);
-        jvmtiDeallocate(info->allowed_peers);
-        jvmtiDeallocate(info);
+        freeTransportInfo(info);
     } else {
         /*
          * Note that we don't attempt to do a launch here. Launching
@@ -614,7 +629,7 @@
              /* The name, address and allowed_peers fields in 'info'
               * are not allocated in the non-server case so
               * they do not need to be freed. */
-             jvmtiDeallocate(info);
+             freeTransportInfo(info);
              return serror;
          }