From 5401e8493b8b16ac13191ec9f8d649650e8d195d Mon Sep 17 00:00:00 2001
Date: Thu, 21 May 2026 18:36:28 +0800
Subject: fix memory leak in jBolt

---
 src/hotspot/share/jbolt/jBoltCallGraph.cpp | 4 ++--
 src/hotspot/share/jbolt/jBoltManager.cpp   | 9 +++++++++
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/hotspot/share/jbolt/jBoltCallGraph.cpp b/src/hotspot/share/jbolt/jBoltCallGraph.cpp
index c171215ad..ccc4ac335 100644
--- a/src/hotspot/share/jbolt/jBoltCallGraph.cpp
+++ b/src/hotspot/share/jbolt/jBoltCallGraph.cpp
@@ -191,14 +191,14 @@ JBoltCallGraph& JBoltCallGraph::callgraph_instance() {
 }
 
 void JBoltCallGraph::add_func(JBoltFunc* func) {
-  if (!(UseJBolt && JBoltManager::reorder_phase_is_profiling_or_waiting())) return;
   JBoltCluster* cluster = find_cluster(func);
   assert(cluster != NULL, "invariant");
+  delete func;
 }
 
 void JBoltCallGraph::add_call(JBoltCall* call) {
-  if (!(UseJBolt && JBoltManager::reorder_phase_is_profiling_or_waiting())) return;
   add_call_to_calls(_calls, call);
+  delete call;
 }
 
 uintptr_t data_layout_jbolt[] = {
diff --git a/src/hotspot/share/jbolt/jBoltManager.cpp b/src/hotspot/share/jbolt/jBoltManager.cpp
index 76be3f6ed..69972a101 100644
--- a/src/hotspot/share/jbolt/jBoltManager.cpp
+++ b/src/hotspot/share/jbolt/jBoltManager.cpp
@@ -347,6 +347,8 @@ void JBoltManager::construct_stacktrace(const JfrStackTrace& stacktrace) {
 
   JBoltFunc **tempfunc = NULL;
 
+  GrowableArray<JBoltFunc*>* funcs = create_growable_array<JBoltFunc*>();
+
   for (u4 i = 0; i < max_frames; ++i) {
     const JfrStackFrame& frame = frames[topFrameIndex + i];
 
@@ -376,7 +378,13 @@ void JBoltManager::construct_stacktrace(const JfrStackTrace& stacktrace) {
       func = NULL;
       break;
     }
+    funcs->append(func);
+  }
+
+  for (int i = 0; i < funcs->length(); ++i) {
+    delete funcs->at(i);
   }
+  delete funcs;
 
   log_trace(jbolt)(
     "StackTrace hash - %u hotcount - %d\n==============================\n", stacktrace.hash(), stacktrace.hotcount());
@@ -397,6 +405,7 @@ void JBoltManager::construct_cg_once() {
     const JfrStackTraceRepository& repository = JfrStackTraceRepository::instance();
 
     if (repository.get_entries_count_jbolt() == 0) {
+      delete traces;
       return;
     }
 
-- 
2.17.1