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 <mark@chromium.org>.

git-svn-id: http://webrtc.googlecode.com/svn/trunk@6131 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
fischman@webrtc.org 2014-05-13 22:18:48 +00:00
parent c14f521b1b
commit d8af5b51c0

View File

@ -48,6 +48,7 @@
#if defined(IOS) || defined(OSX)
#include <mach/mach_host.h>
#include <mach/mach_init.h>
#include <mach/mach_port.h>
#include <mach/host_info.h>
#include <mach/task.h>
#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<host_info_t>(&cpu_info),
&info_count)) {
kern_return_t kr = host_statistics(mach_host, HOST_CPU_LOAD_INFO,
reinterpret_cast<host_info_t>(&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;
}