[Openmp-commits] [PATCH] D60729: Fixed memory leak reported in https://bugs.llvm.org/show_bug.cgi?id=41494

Andrey Churbanov via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Mon Apr 15 11:51:45 PDT 2019

AndreyChurbanov created this revision.
AndreyChurbanov added reviewers: tlwilmar, jlpeyton, hbae.
AndreyChurbanov added a project: OpenMP.
Herald added subscribers: openmp-commits, jdoerfert.

Might be a bit too verbose (not sure if we need to check that the number of threads reduced to 0), but it fixes the memory leak(s) for me.  Multiple leaks were possible in case of multiple foreign threads registered into the library.

  rOMP OpenMP



Index: runtime/src/kmp_runtime.cpp
--- runtime/src/kmp_runtime.cpp
+++ runtime/src/kmp_runtime.cpp
@@ -3982,12 +3982,18 @@
         __kmp_nth - 1); // __kmp_reap_thread will decrement __kmp_all_nth.
-  root->r.r_uber_thread->th.th_cg_roots->cg_nthreads--;
+  i = root->r.r_uber_thread->th.th_cg_roots->cg_nthreads--;
   KA_TRACE(100, ("__kmp_reset_root: Thread %p decrement cg_nthreads on node %p"
                  " to %d\n",
                  root->r.r_uber_thread, root->r.r_uber_thread->th.th_cg_roots,
+  if (i == 1) {
+    // need to free contention group structure
+    KMP_DEBUG_ASSERT(root->r.r_uber_thread == root->r.r_uber_thread->th.th_cg_roots->cg_root);
+    KMP_DEBUG_ASSERT(root->r.r_uber_thread->th.th_cg_roots->up == NULL);
+    __kmp_free(root->r.r_uber_thread->th.th_cg_roots);
+    root->r.r_uber_thread->th.th_cg_roots = NULL;
+  }
   __kmp_reap_thread(root->r.r_uber_thread, 1);
   // We canot put root thread to __kmp_thread_pool, so we have to reap it istead

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60729.195230.patch
Type: text/x-patch
Size: 1151 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20190415/d730e9b9/attachment.bin>

More information about the Openmp-commits mailing list