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