changeset 64:c5c05ec81fea

Bug 3308: Valgrind reports some memory issues Reviewed-by: ykubota https://github.com/HeapStats/heapstats/pull/81
author Yasumasa Suenaga <yasuenag@gmail.com>
date Sun, 29 Jan 2017 21:49:50 +0900
parents 2836f23c1fbb
children ef928edf1d20
files agent/src/classContainer.cpp agent/src/snapShotMain.cpp agent/src/trapSender.hpp agent/src/util.cpp
diffstat 4 files changed, 38 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/agent/src/classContainer.cpp	Tue Jan 24 11:47:47 2017 +0900
+++ b/agent/src/classContainer.cpp	Sun Jan 29 21:49:50 2017 +0900
@@ -242,6 +242,8 @@
         free(cur->className);
         free(cur);
     }
+
+    atomic_inc(&result->numRefs, 1);
     return result;
 }
 
@@ -304,7 +306,6 @@
             try {
                 /* Append class data. */
                 (*classMap)[klassOop] = objData;
-                atomic_inc(&objData->numRefs, 1);
             } catch(...) {
                 /*
                  * Maybe failed to allocate memory at "std::map::operator[]".
@@ -415,9 +416,14 @@
             cur != classMap->end(); ++cur) {
             TObjectData *pos = (*cur).second;
             
-            if ((pos != NULL) && (atomic_get(&pos->numRefs) == 0)) {
-              free(pos->className);
-              free(pos);
+            if (likely(pos != NULL)) {
+              /* Decrement reference from this class map. */
+              atomic_inc(&pos->numRefs, -1);
+
+              if (atomic_get(&pos->numRefs) == 0) {
+                free(pos->className);
+                free(pos);
+              }
             }
         }
         
--- a/agent/src/snapShotMain.cpp	Tue Jan 24 11:47:47 2017 +0900
+++ b/agent/src/snapShotMain.cpp	Sun Jan 29 21:49:50 2017 +0900
@@ -915,7 +915,7 @@
     TSnapShotContainer *snapshot = popSnapShotQueue();
     /* Output all waiting snapshot. */
     while (snapshot != NULL) {
-        
+        snapshot->setTotalSize(jvmInfo->getTotalMemory());
         notifySnapShot(snapshot);
         snapshot = popSnapShotQueue();
     }
@@ -982,17 +982,27 @@
  * \param env [in] JNI environment object.
  */
 void onAgentFinalForSnapShot(JNIEnv *env) {
+    /* Destroy SnapShot Processor instance. */
+    delete snapShotProcessor;
+    snapShotProcessor = NULL;
+
+    /*
+     * Delete snapshot instances
+     */
+    if (snapshotByCMS != NULL) {
+      TSnapShotContainer::releaseInstance(snapshotByCMS);
+    }
+    if (snapshotByGC != NULL) {
+      TSnapShotContainer::releaseInstance(snapshotByGC);
+    }
+
+    /* Finalize and deallocate old snapshot containers. */
+    TSnapShotContainer::globalFinalize();
     
     /* Destroy object that is for snapshot. */
     delete clsContainer;
     clsContainer = NULL;
     
-    delete snapShotProcessor;
-    snapShotProcessor = NULL;
-    
-    /* Finalize and deallocate old snapshot containers. */
-    TSnapShotContainer::globalFinalize();
-    
     /* Destroy object that is each snapshot trigger. */
     delete gcWatcher;
     gcWatcher = NULL;
--- a/agent/src/trapSender.hpp	Tue Jan 24 11:47:47 2017 +0900
+++ b/agent/src/trapSender.hpp	Sun Jan 29 21:49:50 2017 +0900
@@ -1,7 +1,7 @@
 /*!
  * \file trapSender.hpp
  * \brief This file is used to send SNMP trap.
- * Copyright (C) 2011-2015 Nippon Telegraph and Telephone Corporation
+ * Copyright (C) 2011-2017 Nippon Telegraph and Telephone Corporation
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -227,6 +227,8 @@
             }
             /* Close and free SNMP session. */
             snmp_close(&session);
+            free(session.peername);
+            free(session.community);
             
             /* Unlock to use in multi-thread. */
             EXIT_PTHREAD_SECTION(&senderMutex)
--- a/agent/src/util.cpp	Tue Jan 24 11:47:47 2017 +0900
+++ b/agent/src/util.cpp	Sun Jan 29 21:49:50 2017 +0900
@@ -987,7 +987,14 @@
     }
     
     /* Clean up. */
-    
+    for (int Cnt = 0; Cnt < count; Cnt++) {
+      jvmti->Deallocate((unsigned char *)events[Cnt].id);
+      jvmti->Deallocate((unsigned char *)events[Cnt].short_description);
+      for (int Cnt2 = 0; Cnt2 < events[Cnt].param_count; Cnt2++) {
+        jvmti->Deallocate((unsigned char *)events[Cnt].params[Cnt2].name);
+      }
+      jvmti->Deallocate((unsigned char *)events[Cnt].params);
+    }
     jvmti->Deallocate((unsigned char *)events);
     return ret;
 }