Do not leak the Mach thread port in GetThreadCpuTimeNanos().
Bug: chromium:879307
Change-Id: Ia6b5b3ea4684354d8a21dc85e43f67166832cc19
Reviewed-on: https://webrtc-review.googlesource.com/96980
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Robert Sesek <rsesek@chromium.org>
Cr-Commit-Position: refs/heads/master@{#24554}
diff --git a/rtc_base/cpu_time.cc b/rtc_base/cpu_time.cc
index f25b506..de4a6bd 100644
--- a/rtc_base/cpu_time.cc
+++ b/rtc_base/cpu_time.cc
@@ -16,6 +16,7 @@
#include <time.h>
#elif defined(WEBRTC_MAC)
#include <mach/mach_init.h>
+#include <mach/mach_port.h>
#include <mach/thread_act.h>
#include <mach/thread_info.h>
#include <sys/resource.h>
@@ -81,10 +82,13 @@
RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
}
#elif defined(WEBRTC_MAC)
+ mach_port_t thread_port = mach_thread_self();
thread_basic_info_data_t info;
mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
- if (thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)&info,
- &count) == KERN_SUCCESS) {
+ kern_return_t kr =
+ thread_info(thread_port, THREAD_BASIC_INFO, (thread_info_t)&info, &count);
+ mach_port_deallocate(mach_task_self(), thread_port);
+ if (kr == KERN_SUCCESS) {
return info.user_time.seconds * kNumNanosecsPerSec +
info.user_time.microseconds * kNumNanosecsPerMicrosec;
} else {