[Lldb-commits] [lldb] r164152 - in /lldb/trunk: source/Host/common/Host.cpp tools/debugserver/source/DNBLog.cpp

Greg Clayton gclayton at apple.com
Tue Sep 18 11:19:50 PDT 2012


Author: gclayton
Date: Tue Sep 18 13:19:49 2012
New Revision: 164152

URL: http://llvm.org/viewvc/llvm-project?rev=164152&view=rev
Log:
<rdar://problem/12219840>

Don't leak mach ports when calling "mach_thread_self()".


Modified:
    lldb/trunk/source/Host/common/Host.cpp
    lldb/trunk/tools/debugserver/source/DNBLog.cpp

Modified: lldb/trunk/source/Host/common/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/Host.cpp?rev=164152&r1=164151&r2=164152&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/Host.cpp (original)
+++ lldb/trunk/source/Host/common/Host.cpp Tue Sep 18 13:19:49 2012
@@ -39,6 +39,7 @@
 #include <dispatch/dispatch.h>
 #include <libproc.h>
 #include <mach-o/dyld.h>
+#include <mach/mach_port.h>
 #include <sys/sysctl.h>
 
 
@@ -430,7 +431,12 @@
 Host::GetCurrentThreadID()
 {
 #if defined (__APPLE__)
-    return ::mach_thread_self();
+    // Calling "mach_port_deallocate()" bumps the reference count on the thread
+    // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
+    // count.
+    thread_port_t thread_self = mach_thread_self();
+    mach_port_deallocate(mach_task_self(), thread_self);
+    return thread_self;
 #elif defined(__FreeBSD__)
     return lldb::tid_t(pthread_getthreadid_np());
 #else

Modified: lldb/trunk/tools/debugserver/source/DNBLog.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/debugserver/source/DNBLog.cpp?rev=164152&r1=164151&r2=164152&view=diff
==============================================================================
--- lldb/trunk/tools/debugserver/source/DNBLog.cpp (original)
+++ lldb/trunk/tools/debugserver/source/DNBLog.cpp Tue Sep 18 13:19:49 2012
@@ -185,13 +185,21 @@
                 timersub (&tv, &g_timeval, &delta);
             }
             g_timeval = tv;
+            
+            // Calling "mach_port_deallocate()" bumps the reference count on the thread
+            // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
+            // count.
+            thread_port_t thread_self = mach_thread_self();
+
             _DNBLog (DNBLOG_FLAG_THREADED, "%u +%lu.%06u sec [%4.4x/%4.4x]: %s", 
                      ++g_message_id, 
                      delta.tv_sec, 
                      delta.tv_usec,             
                      getpid(), 
-                     mach_thread_self(), 
+                     thread_self, 
                      arg_msg);
+
+            mach_port_deallocate(mach_task_self(), thread_self);
             free (arg_msg);
         }
     }
@@ -230,13 +238,22 @@
                 timersub (&tv, &g_timeval, &delta);
             }
             g_timeval = tv;
-            _DNBLog (DNBLOG_FLAG_THREADED, "%u +%lu.%06u sec [%4.4x/%4.4x]: %s", 
+
+            // Calling "mach_port_deallocate()" bumps the reference count on the thread
+            // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
+            // count.
+            thread_port_t thread_self = mach_thread_self();
+
+            _DNBLog (DNBLOG_FLAG_THREADED, "%u +%lu.%06u sec [%4.4x/%4.4x]: %s",
                      ++g_message_id, 
                      delta.tv_sec, 
                      delta.tv_usec, 
                      getpid(), 
-                     mach_thread_self(), 
+                     thread_self, 
                      arg_msg);
+
+            mach_port_deallocate(mach_task_self(), thread_self);
+
             free (arg_msg);
         }
     }





More information about the lldb-commits mailing list