From b9a7cead1cdf588b9b00ec46f38a727b14681c5b Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Fri, 26 Mar 2010 20:23:06 +0000 Subject: Fixes a leak in ThreadLocal. --- include/gtest/internal/gtest-port.h | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'include/gtest/internal/gtest-port.h') diff --git a/include/gtest/internal/gtest-port.h b/include/gtest/internal/gtest-port.h index 66cfe46e..a2a62be9 100644 --- a/include/gtest/internal/gtest-port.h +++ b/include/gtest/internal/gtest-port.h @@ -1084,10 +1084,19 @@ extern "C" inline void DeleteThreadLocalValue(void* value_holder) { // // The template type argument T must have a public copy constructor. // In addition, the default ThreadLocal constructor requires T to have -// a public default constructor. An object managed by a ThreadLocal -// instance for a thread is guaranteed to exist at least until the -// earliest of the two events: (a) the thread terminates or (b) the -// ThreadLocal object is destroyed. +// a public default constructor. +// +// An object managed for a thread by a ThreadLocal instance is deleted +// when the thread exits. Or, if the ThreadLocal instance dies in +// that thread, when the ThreadLocal dies. It's the user's +// responsibility to ensure that all other threads using a ThreadLocal +// have exited when it dies, or the per-thread objects for those +// threads will not be deleted. +// +// Google Test only uses global ThreadLocal objects. That means they +// will die after main() has returned. Therefore, no per-thread +// object managed by Google Test will be leaked as long as all threads +// using Google Test have exited when main() returns. template class ThreadLocal { public: @@ -1097,6 +1106,11 @@ class ThreadLocal { default_(value) {} ~ThreadLocal() { + // Destroys the managed object for the current thread, if any. + DeleteThreadLocalValue(pthread_getspecific(key_)); + + // Releases resources associated with the key. This will *not* + // delete managed objects for other threads. GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_)); } @@ -1120,6 +1134,8 @@ class ThreadLocal { static pthread_key_t CreateKey() { pthread_key_t key; + // When a thread exits, DeleteThreadLocalValue() will be called on + // the object managed for that thread. GTEST_CHECK_POSIX_SUCCESS_( pthread_key_create(&key, &DeleteThreadLocalValue)); return key; -- cgit v1.2.3