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}
This commit is contained in:
Robert Sesek
2018-08-30 17:13:44 -04:00
committed by Commit Bot
parent 46d65e2c2c
commit b0800519b0

View File

@ -16,6 +16,7 @@
#include <time.h> #include <time.h>
#elif defined(WEBRTC_MAC) #elif defined(WEBRTC_MAC)
#include <mach/mach_init.h> #include <mach/mach_init.h>
#include <mach/mach_port.h>
#include <mach/thread_act.h> #include <mach/thread_act.h>
#include <mach/thread_info.h> #include <mach/thread_info.h>
#include <sys/resource.h> #include <sys/resource.h>
@ -81,10 +82,13 @@ int64_t GetThreadCpuTimeNanos() {
RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed."; RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
} }
#elif defined(WEBRTC_MAC) #elif defined(WEBRTC_MAC)
mach_port_t thread_port = mach_thread_self();
thread_basic_info_data_t info; thread_basic_info_data_t info;
mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
if (thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)&info, kern_return_t kr =
&count) == KERN_SUCCESS) { 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 + return info.user_time.seconds * kNumNanosecsPerSec +
info.user_time.microseconds * kNumNanosecsPerMicrosec; info.user_time.microseconds * kNumNanosecsPerMicrosec;
} else { } else {