OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef BASE_LEAK_TRACKER_H_ |
| 6 #define BASE_LEAK_TRACKER_H_ |
| 7 |
| 8 #ifndef NDEBUG |
| 9 #include "base/debug_util.h" |
| 10 #include "base/linked_list.h" |
| 11 #include "base/logging.h" |
| 12 #endif |
| 13 |
| 14 // LeakTracker is a debug helper to verify that all instances of a class |
| 15 // have been destroyed. |
| 16 // |
| 17 // It is particularly useful for classes that are bound to a single thread -- |
| 18 // before destroying that thread, one can check that there are no remaining |
| 19 // instances of that class. |
| 20 // |
| 21 // For example, to enable leak tracking for class URLRequest, start by |
| 22 // adding a member variable of type LeakTracker<URLRequest>. |
| 23 // |
| 24 // class URLRequest { |
| 25 // ... |
| 26 // private: |
| 27 // base::LeakTracker<URLRequest> leak_tracker_; |
| 28 // }; |
| 29 // |
| 30 // |
| 31 // Next, when we believe all instances of URLRequest have been deleted: |
| 32 // |
| 33 // LeakTracker<URLRequest>::CheckForLeaks(); |
| 34 // |
| 35 // Should the check fail (because there are live instances of URLRequest), |
| 36 // then the allocation callstack for each leaked instances is dumped to |
| 37 // the error log. |
| 38 // |
| 39 // In RELEASE mode the check has no effect. |
| 40 |
| 41 namespace base { |
| 42 |
| 43 #ifdef NDEBUG |
| 44 |
| 45 // In release mode we do nothing. |
| 46 template<typename T> |
| 47 class LeakTracker { |
| 48 public: |
| 49 static void CheckForLeaks() {} |
| 50 static int NumLiveInstances() { return -1; } |
| 51 }; |
| 52 |
| 53 #else |
| 54 |
| 55 // In debug mode we track where the object was allocated from. |
| 56 |
| 57 template<typename T> |
| 58 class LeakTracker : public LinkNode<LeakTracker<T> > { |
| 59 public: |
| 60 LeakTracker() { |
| 61 instances()->Append(this); |
| 62 } |
| 63 |
| 64 ~LeakTracker() { |
| 65 this->RemoveFromList(); |
| 66 } |
| 67 |
| 68 static void CheckForLeaks() { |
| 69 // Walk the allocation list and print each entry it contains. |
| 70 int count = 0; |
| 71 for (LinkNode<LeakTracker<T> >* node = instances()->head(); |
| 72 node != instances()->end(); |
| 73 node = node->next()) { |
| 74 ++count; |
| 75 LOG(ERROR) << "Leaked " << node << " which was allocated by:"; |
| 76 node->value()->allocation_stack_.PrintBacktrace(); |
| 77 } |
| 78 DCHECK_EQ(0, count); |
| 79 } |
| 80 |
| 81 static int NumLiveInstances() { |
| 82 // Walk the allocation list and count how many entries it has. |
| 83 int count = 0; |
| 84 for (LinkNode<LeakTracker<T> >* node = instances()->head(); |
| 85 node != instances()->end(); |
| 86 node = node->next()) { |
| 87 ++count; |
| 88 } |
| 89 return count; |
| 90 } |
| 91 |
| 92 private: |
| 93 // Each specialization of LeakTracker gets its own static storage. |
| 94 static LinkedList<LeakTracker<T> >* instances() { |
| 95 static LinkedList<LeakTracker<T> > list; |
| 96 return &list; |
| 97 } |
| 98 |
| 99 StackTrace allocation_stack_; |
| 100 }; |
| 101 |
| 102 #endif // NDEBUG |
| 103 |
| 104 } // namespace base |
| 105 |
| 106 #endif // BASE_LEAK_TRACKER_H_ |
OLD | NEW |