diff options
author | ericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-11 21:38:54 +0000 |
---|---|---|
committer | ericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-11 21:38:54 +0000 |
commit | 48ca901238509dd4c5f7bb30c66ef36f5dafc828 (patch) | |
tree | 34a2d3976009fd351aa91e9257dd6522907928dd /base/leak_tracker_unittest.cc | |
parent | 51e9e931b1d9c36605c6336a3002fda22f023b36 (diff) | |
download | chromium_src-48ca901238509dd4c5f7bb30c66ef36f5dafc828.zip chromium_src-48ca901238509dd4c5f7bb30c66ef36f5dafc828.tar.gz chromium_src-48ca901238509dd4c5f7bb30c66ef36f5dafc828.tar.bz2 |
Add checks to DEBUG mode that no instance of URLRequest or URLFetcher survives the destruction of the IO thread.
This checking is done by introducing a new helper class to base called LeakTracker. Classes that you want to check for leaks just need to extend LeakTracker.
The reason I am picking on URLFetcher / URLRequest, is I believe we have a bug that is making an instance of URLFetcher to outlive the IO thread.
This causes various sorts of badness.
For example:
If URLFetcher survives the IO thread, then URLRequestContext remains referenced and therefore also survives IO thread. In turn HostResolverImpl survives the IO thread, so any outstanding resolve requests are NOT cancelled before the IO thread is decomissioned. So now, when the worker thread doing the DNS resolve finally finishes (assuming it finishes before the rogue URLRequest is destroyed), it post the result to a defunct message loop. KAB00m! (http://crbug.com/15513)
Moreover, I believe we hit this same problem sporadically in AutomationProxyTest.AutocompleteGetSetText -- the test is flaky on the buildbots, and I've seen DCHECKs which suggest it is related to this issue.
BUG=http://crbug.com/18372
Review URL: http://codereview.chromium.org/160447
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@23084 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/leak_tracker_unittest.cc')
-rw-r--r-- | base/leak_tracker_unittest.cc | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/base/leak_tracker_unittest.cc b/base/leak_tracker_unittest.cc new file mode 100644 index 0000000..8f0e2f2 --- /dev/null +++ b/base/leak_tracker_unittest.cc @@ -0,0 +1,108 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/leak_tracker.h" +#include "base/scoped_ptr.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +class ClassA { + private: + base::LeakTracker<ClassA> leak_tracker_; +}; + +class ClassB { + private: + base::LeakTracker<ClassB> leak_tracker_; +}; + +#ifdef NDEBUG + +// In RELEASE mode, leak tracking is disabled. +TEST(LeakTrackerTest, ReleaseMode) { + EXPECT_EQ(-1, base::LeakTracker<ClassA>::NumLiveInstances()); + EXPECT_EQ(-1, base::LeakTracker<ClassB>::NumLiveInstances()); + + // Use scoped_ptr so compiler doesn't complain about unused variables. + scoped_ptr<ClassA> a1(new ClassA); + scoped_ptr<ClassB> b1(new ClassB); + scoped_ptr<ClassB> b2(new ClassB); + + EXPECT_EQ(-1, base::LeakTracker<ClassA>::NumLiveInstances()); + EXPECT_EQ(-1, base::LeakTracker<ClassB>::NumLiveInstances()); +} + +#else + +// In DEBUG mode, leak tracking should work. +TEST(LeakTrackerTest, DebugMode) { + { + ClassA a1; + + EXPECT_EQ(1, base::LeakTracker<ClassA>::NumLiveInstances()); + EXPECT_EQ(0, base::LeakTracker<ClassB>::NumLiveInstances()); + + ClassB b1; + ClassB b2; + + EXPECT_EQ(1, base::LeakTracker<ClassA>::NumLiveInstances()); + EXPECT_EQ(2, base::LeakTracker<ClassB>::NumLiveInstances()); + + scoped_ptr<ClassA> a2(new ClassA); + + EXPECT_EQ(2, base::LeakTracker<ClassA>::NumLiveInstances()); + EXPECT_EQ(2, base::LeakTracker<ClassB>::NumLiveInstances()); + + a2.reset(); + + EXPECT_EQ(1, base::LeakTracker<ClassA>::NumLiveInstances()); + EXPECT_EQ(2, base::LeakTracker<ClassB>::NumLiveInstances()); + } + + EXPECT_EQ(0, base::LeakTracker<ClassA>::NumLiveInstances()); + EXPECT_EQ(0, base::LeakTracker<ClassB>::NumLiveInstances()); +} + +// Try some orderings of create/remove to hit different cases in the linked-list +// assembly. +TEST(LeakTrackerTest, DebugMode_LinkedList) { + EXPECT_EQ(0, base::LeakTracker<ClassB>::NumLiveInstances()); + + scoped_ptr<ClassA> a1(new ClassA); + scoped_ptr<ClassA> a2(new ClassA); + scoped_ptr<ClassA> a3(new ClassA); + scoped_ptr<ClassA> a4(new ClassA); + + EXPECT_EQ(4, base::LeakTracker<ClassA>::NumLiveInstances()); + + // Remove the head of the list (a1). + a1.reset(); + EXPECT_EQ(3, base::LeakTracker<ClassA>::NumLiveInstances()); + + // Remove the tail of the list (a4). + a4.reset(); + EXPECT_EQ(2, base::LeakTracker<ClassA>::NumLiveInstances()); + + // Append to the new tail of the list (a3). + scoped_ptr<ClassA> a5(new ClassA); + EXPECT_EQ(3, base::LeakTracker<ClassA>::NumLiveInstances()); + + a2.reset(); + a3.reset(); + + EXPECT_EQ(1, base::LeakTracker<ClassA>::NumLiveInstances()); + + a5.reset(); + EXPECT_EQ(0, base::LeakTracker<ClassA>::NumLiveInstances()); +} + +TEST(LeakTrackerTest, DebugMode_NoOpCheckForLeaks) { + // There are no live instances of ClassA, so this should do nothing. + base::LeakTracker<ClassA>::CheckForLeaks(); +} + +#endif // NDEBUG + +} // namespace |