From 5a236f79082386a5c9edb6da07d02b3fd1d3cd25 Mon Sep 17 00:00:00 2001 From: "eroman@chromium.org" Date: Wed, 16 Sep 2009 21:14:08 +0000 Subject: Make LeakTracker be enabled using ENABLE_LEAK_TRACKER, rather than NDEBUG. This way it is easy to toggle it on in release builds. Review URL: http://codereview.chromium.org/196130 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@26389 0039d316-1c4b-4281-b951-d872f2087c98 --- base/leak_tracker.h | 21 +++++++++++++-------- base/leak_tracker_unittest.cc | 15 +++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) (limited to 'base') diff --git a/base/leak_tracker.h b/base/leak_tracker.h index d289691e..a5d105a 100644 --- a/base/leak_tracker.h +++ b/base/leak_tracker.h @@ -5,13 +5,18 @@ #ifndef BASE_LEAK_TRACKER_H_ #define BASE_LEAK_TRACKER_H_ +// Only enable leak tracking in debug builds. #ifndef NDEBUG +#define ENABLE_LEAK_TRACKER +#endif + +#ifdef ENABLE_LEAK_TRACKER #include "base/debug_util.h" #include "base/linked_list.h" #include "base/logging.h" -#endif +#endif // ENABLE_LEAK_TRACKER -// LeakTracker is a debug helper to verify that all instances of a class +// LeakTracker is a helper to verify that all instances of a class // have been destroyed. // // It is particularly useful for classes that are bound to a single thread -- @@ -36,13 +41,13 @@ // then the allocation callstack for each leaked instances is dumped to // the error log. // -// In RELEASE mode the check has no effect. +// If ENABLE_LEAK_TRACKER is not defined, then the check has no effect. namespace base { -#ifdef NDEBUG +#ifndef ENABLE_LEAK_TRACKER -// In release mode we do nothing. +// If leak tracking is disabled, do nothing. template class LeakTracker { public: @@ -52,7 +57,7 @@ class LeakTracker { #else -// In debug mode we track where the object was allocated from. +// If leak tracking is enabled we track where the object was allocated from. template class LeakTracker : public LinkNode > { @@ -75,7 +80,7 @@ class LeakTracker : public LinkNode > { LOG(ERROR) << "Leaked " << node << " which was allocated by:"; node->value()->allocation_stack_.PrintBacktrace(); } - DCHECK_EQ(0, count); + CHECK(0 == count); } static int NumLiveInstances() { @@ -99,7 +104,7 @@ class LeakTracker : public LinkNode > { StackTrace allocation_stack_; }; -#endif // NDEBUG +#endif // ENABLE_LEAK_TRACKER } // namespace base diff --git a/base/leak_tracker_unittest.cc b/base/leak_tracker_unittest.cc index 8f0e2f2..0217b17 100644 --- a/base/leak_tracker_unittest.cc +++ b/base/leak_tracker_unittest.cc @@ -18,10 +18,10 @@ class ClassB { base::LeakTracker leak_tracker_; }; -#ifdef NDEBUG +#ifndef ENABLE_LEAK_TRACKER -// In RELEASE mode, leak tracking is disabled. -TEST(LeakTrackerTest, ReleaseMode) { +// If leak tracking is disabled, we should do nothing. +TEST(LeakTrackerTest, NotEnabled) { EXPECT_EQ(-1, base::LeakTracker::NumLiveInstances()); EXPECT_EQ(-1, base::LeakTracker::NumLiveInstances()); @@ -36,8 +36,7 @@ TEST(LeakTrackerTest, ReleaseMode) { #else -// In DEBUG mode, leak tracking should work. -TEST(LeakTrackerTest, DebugMode) { +TEST(LeakTrackerTest, Basic) { { ClassA a1; @@ -67,7 +66,7 @@ TEST(LeakTrackerTest, DebugMode) { // Try some orderings of create/remove to hit different cases in the linked-list // assembly. -TEST(LeakTrackerTest, DebugMode_LinkedList) { +TEST(LeakTrackerTest, LinkedList) { EXPECT_EQ(0, base::LeakTracker::NumLiveInstances()); scoped_ptr a1(new ClassA); @@ -98,11 +97,11 @@ TEST(LeakTrackerTest, DebugMode_LinkedList) { EXPECT_EQ(0, base::LeakTracker::NumLiveInstances()); } -TEST(LeakTrackerTest, DebugMode_NoOpCheckForLeaks) { +TEST(LeakTrackerTest, NoOpCheckForLeaks) { // There are no live instances of ClassA, so this should do nothing. base::LeakTracker::CheckForLeaks(); } -#endif // NDEBUG +#endif // ENABLE_LEAK_TRACKER } // namespace -- cgit v1.1