leak_tracker.h 3.9 KB

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