From 767998dfadaf87c1193d51cf5847a7f147c6e1f7 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Thu, 9 Apr 2009 02:57:38 +0000 Subject: Makes the Python tests more stable (by Vlad Losev); fixes a memory leak in GetThreadCount() on Mac (by Vlad Losev); improves fuse_gtest_files.py to support fusing Google Mock files (by Zhanyong Wan). git-svn-id: http://googletest.googlecode.com/svn/trunk@238 861a406c-534a-0410-8894-cb66d6ee9925 --- src/gtest-port.cc | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'src/gtest-port.cc') diff --git a/src/gtest-port.cc b/src/gtest-port.cc index 193f532..3c1e80c 100644 --- a/src/gtest-port.cc +++ b/src/gtest-port.cc @@ -45,6 +45,7 @@ #if GTEST_OS_MAC #include #include +#include #endif // GTEST_OS_MAC #ifdef _WIN32_WCE @@ -74,22 +75,37 @@ const int kStdErrFileno = 2; const int kStdErrFileno = STDERR_FILENO; #endif // _MSC_VER +#if GTEST_OS_MAC + // Returns the number of threads running in the process, or 0 to indicate that // we cannot detect it. size_t GetThreadCount() { -#if GTEST_OS_MAC + const task_t task = mach_task_self(); mach_msg_type_number_t thread_count; - thread_act_port_array_t thread_list; - kern_return_t status = task_threads(mach_task_self(), - &thread_list, &thread_count); - return status == KERN_SUCCESS ? static_cast(thread_count) : 0; + thread_act_array_t thread_list; + const kern_return_t status = task_threads(task, &thread_list, &thread_count); + if (status == KERN_SUCCESS) { + // task_threads allocates resources in thread_list and we need to free them + // to avoid leaks. + vm_deallocate(task, + reinterpret_cast(thread_list), + sizeof(thread_t) * thread_count); + return static_cast(thread_count); + } else { + return 0; + } +} + #else + +size_t GetThreadCount() { // There's no portable way to detect the number of threads, so we just // return 0 to indicate that we cannot detect it. return 0; -#endif // GTEST_OS_MAC } +#endif // GTEST_OS_MAC + #if GTEST_USES_POSIX_RE // Implements RE. Currently only needed for death tests. -- cgit v1.2.3