From d8af5b51c0ce59bbb8cb6ff753ed507c90eac93a Mon Sep 17 00:00:00 2001 From: "fischman@webrtc.org" Date: Tue, 13 May 2014 22:18:48 +0000 Subject: [PATCH] Deallocate the result of mach_host_self() when done with it, fixing a port leak. The port rights obtained by mach_host_self() and mach_thread_self() need to be deallocated with mach_port_deallocate(). They consume finite system-wide resources. This is in contrast to mach_task_self(), which is a macro that wraps an extern global variable, and must not be deallocated. http://crbug.com/105513 shows the sorts of problems that can occur when these aren't properly deallocated. R=fischman@webrtc.org, tkchin@webrtc.org Review URL: https://webrtc-codereview.appspot.com/15469004 Patch from Mark Mentovai . git-svn-id: http://webrtc.googlecode.com/svn/trunk@6131 4adac7df-926f-26a2-2b94-8c16560cd09d --- talk/base/cpumonitor.cc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/talk/base/cpumonitor.cc b/talk/base/cpumonitor.cc index e9b481fdb..aaec77208 100644 --- a/talk/base/cpumonitor.cc +++ b/talk/base/cpumonitor.cc @@ -48,6 +48,7 @@ #if defined(IOS) || defined(OSX) #include #include +#include #include #include #endif // defined(IOS) || defined(OSX) @@ -241,11 +242,14 @@ float CpuSampler::GetSystemLoad() { #endif // WIN32 #if defined(IOS) || defined(OSX) + mach_port_t mach_host = mach_host_self(); host_cpu_load_info_data_t cpu_info; mach_msg_type_number_t info_count = HOST_CPU_LOAD_INFO_COUNT; - if (KERN_SUCCESS != host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, - reinterpret_cast(&cpu_info), - &info_count)) { + kern_return_t kr = host_statistics(mach_host, HOST_CPU_LOAD_INFO, + reinterpret_cast(&cpu_info), + &info_count); + mach_port_deallocate(mach_task_self(), mach_host); + if (KERN_SUCCESS != kr) { LOG(LS_ERROR) << "::host_statistics() failed"; return 0.f; }