leak_tracker.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (c) 2012 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. #ifndef BASE_DEBUG_LEAK_TRACKER_H_
  5. #define BASE_DEBUG_LEAK_TRACKER_H_
  6. #include <stddef.h>
  7. #include "build/build_config.h"
  8. // Only enable leak tracking in non-uClibc debug builds.
  9. #if !defined(NDEBUG) && !defined(__UCLIBC__)
  10. #define ENABLE_LEAK_TRACKER
  11. #endif
  12. #ifdef ENABLE_LEAK_TRACKER
  13. #include "base/containers/linked_list.h"
  14. #include "base/debug/stack_trace.h"
  15. #include "base/logging.h"
  16. #endif // ENABLE_LEAK_TRACKER
  17. // LeakTracker is a helper to verify that all instances of a class
  18. // have been destroyed.
  19. //
  20. // It is particularly useful for classes that are bound to a single thread --
  21. // before destroying that thread, one can check that there are no remaining
  22. // instances of that class.
  23. //
  24. // For example, to enable leak tracking for class net::URLRequest, start by
  25. // adding a member variable of type LeakTracker<net::URLRequest>.
  26. //
  27. // class URLRequest {
  28. // ...
  29. // private:
  30. // base::LeakTracker<URLRequest> leak_tracker_;
  31. // };
  32. //
  33. //
  34. // Next, when we believe all instances of net::URLRequest have been deleted:
  35. //
  36. // LeakTracker<net::URLRequest>::CheckForLeaks();
  37. //
  38. // Should the check fail (because there are live instances of net::URLRequest),
  39. // then the allocation callstack for each leaked instances is dumped to
  40. // the error log.
  41. //
  42. // If ENABLE_LEAK_TRACKER is not defined, then the check has no effect.
  43. namespace base {
  44. namespace debug {
  45. #ifndef ENABLE_LEAK_TRACKER
  46. // If leak tracking is disabled, do nothing.
  47. template<typename T>
  48. class LeakTracker {
  49. public:
  50. // This destructor suppresses warnings about instances of this class not being
  51. // used.
  52. ~LeakTracker() {}
  53. static void CheckForLeaks() {}
  54. static int NumLiveInstances() { return -1; }
  55. };
  56. #else
  57. // If leak tracking is enabled we track where the object was allocated from.
  58. template<typename T>
  59. class LeakTracker : public LinkNode<LeakTracker<T> > {
  60. public:
  61. LeakTracker() {
  62. instances()->Append(this);
  63. }
  64. ~LeakTracker() {
  65. this->RemoveFromList();
  66. }
  67. static void CheckForLeaks() {
  68. // Walk the allocation list and print each entry it contains.
  69. size_t count = 0;
  70. // Copy the first 3 leak allocation callstacks onto the stack.
  71. // This way if we hit the CHECK() in a release build, the leak
  72. // information will be available in mini-dump.
  73. const size_t kMaxStackTracesToCopyOntoStack = 3;
  74. StackTrace stacktraces[kMaxStackTracesToCopyOntoStack];
  75. for (LinkNode<LeakTracker<T> >* node = instances()->head();
  76. node != instances()->end();
  77. node = node->next()) {
  78. StackTrace& allocation_stack = node->value()->allocation_stack_;
  79. if (count < kMaxStackTracesToCopyOntoStack)
  80. stacktraces[count] = allocation_stack;
  81. ++count;
  82. if (LOG_IS_ON(ERROR)) {
  83. LOG_STREAM(ERROR) << "Leaked " << node << " which was allocated by:";
  84. allocation_stack.OutputToStream(&LOG_STREAM(ERROR));
  85. }
  86. }
  87. CHECK_EQ(0u, count);
  88. // Hack to keep |stacktraces| and |count| alive (so compiler
  89. // doesn't optimize it out, and it will appear in mini-dumps).
  90. if (count == 0x1234) {
  91. for (size_t i = 0; i < kMaxStackTracesToCopyOntoStack; ++i)
  92. stacktraces[i].Print();
  93. }
  94. }
  95. static int NumLiveInstances() {
  96. // Walk the allocation list and count how many entries it has.
  97. int count = 0;
  98. for (LinkNode<LeakTracker<T> >* node = instances()->head();
  99. node != instances()->end();
  100. node = node->next()) {
  101. ++count;
  102. }
  103. return count;
  104. }
  105. private:
  106. // Each specialization of LeakTracker gets its own static storage.
  107. static LinkedList<LeakTracker<T> >* instances() {
  108. static LinkedList<LeakTracker<T> > list;
  109. return &list;
  110. }
  111. StackTrace allocation_stack_;
  112. };
  113. #endif // ENABLE_LEAK_TRACKER
  114. } // namespace debug
  115. } // namespace base
  116. #endif // BASE_DEBUG_LEAK_TRACKER_H_