Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LeakDetector.hpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2015 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DISTRHO_LEAK_DETECTOR_HPP_INCLUDED
  17. #define DISTRHO_LEAK_DETECTOR_HPP_INCLUDED
  18. #include "../DistrhoUtils.hpp"
  19. START_NAMESPACE_DISTRHO
  20. // -----------------------------------------------------------------------
  21. // The following code was based from juce-core LeakDetector class
  22. // Copyright (C) 2013 Raw Material Software Ltd.
  23. /** A good old-fashioned C macro concatenation helper.
  24. This combines two items (which may themselves be macros) into a single string,
  25. avoiding the pitfalls of the ## macro operator.
  26. */
  27. #define DISTRHO_JOIN_MACRO_HELPER(a, b) a ## b
  28. #define DISTRHO_JOIN_MACRO(item1, item2) DISTRHO_JOIN_MACRO_HELPER(item1, item2)
  29. #ifdef DEBUG
  30. /** This macro lets you embed a leak-detecting object inside a class.\n
  31. To use it, simply declare a DISTRHO_LEAK_DETECTOR(YourClassName) inside a private section
  32. of the class declaration. E.g.
  33. \code
  34. class MyClass
  35. {
  36. public:
  37. MyClass();
  38. void blahBlah();
  39. private:
  40. DISTRHO_LEAK_DETECTOR(MyClass)
  41. };
  42. \endcode
  43. */
  44. # define DISTRHO_LEAK_DETECTOR(ClassName) \
  45. friend class DISTRHO_NAMESPACE::LeakedObjectDetector<ClassName>; \
  46. static const char* getLeakedObjectClassName() noexcept { return #ClassName; } \
  47. DISTRHO_NAMESPACE::LeakedObjectDetector<ClassName> DISTRHO_JOIN_MACRO(leakDetector_, ClassName);
  48. # define DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ClassName) \
  49. DISTRHO_DECLARE_NON_COPY_CLASS(ClassName) \
  50. DISTRHO_LEAK_DETECTOR(ClassName)
  51. #else
  52. /** Don't use leak detection on release builds. */
  53. # define DISTRHO_LEAK_DETECTOR(ClassName)
  54. # define DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ClassName) \
  55. DISTRHO_DECLARE_NON_COPY_CLASS(ClassName)
  56. #endif
  57. //==============================================================================
  58. /**
  59. Embedding an instance of this class inside another class can be used as a low-overhead
  60. way of detecting leaked instances.
  61. This class keeps an internal static count of the number of instances that are
  62. active, so that when the app is shutdown and the static destructors are called,
  63. it can check whether there are any left-over instances that may have been leaked.
  64. To use it, use the DISTRHO_LEAK_DETECTOR macro as a simple way to put one in your
  65. class declaration.
  66. */
  67. template<class OwnerClass>
  68. class LeakedObjectDetector
  69. {
  70. public:
  71. //==============================================================================
  72. LeakedObjectDetector() noexcept { ++(getCounter().numObjects); }
  73. LeakedObjectDetector(const LeakedObjectDetector&) noexcept { ++(getCounter().numObjects); }
  74. ~LeakedObjectDetector() noexcept
  75. {
  76. if (--(getCounter().numObjects) < 0)
  77. {
  78. /** If you hit this, then you've managed to delete more instances of this class than you've
  79. created.. That indicates that you're deleting some dangling pointers.
  80. Note that although this assertion will have been triggered during a destructor, it might
  81. not be this particular deletion that's at fault - the incorrect one may have happened
  82. at an earlier point in the program, and simply not been detected until now.
  83. Most errors like this are caused by using old-fashioned, non-RAII techniques for
  84. your object management. Tut, tut. Always, always use ScopedPointers, OwnedArrays,
  85. ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
  86. */
  87. d_stderr2("*** Dangling pointer deletion! Class: '%s', Count: %i", getLeakedObjectClassName(), getCounter().numObjects);
  88. }
  89. }
  90. private:
  91. //==============================================================================
  92. class LeakCounter
  93. {
  94. public:
  95. LeakCounter() noexcept
  96. : numObjects(0) {}
  97. ~LeakCounter() noexcept
  98. {
  99. if (numObjects > 0)
  100. {
  101. /** If you hit this, then you've leaked one or more objects of the type specified by
  102. the 'OwnerClass' template parameter - the name should have been printed by the line above.
  103. If you're leaking, it's probably because you're using old-fashioned, non-RAII techniques for
  104. your object management. Tut, tut. Always, always use ScopedPointers, OwnedArrays,
  105. ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
  106. */
  107. d_stderr2("*** Leaked objects detected: %i instance(s) of class '%s'", numObjects, getLeakedObjectClassName());
  108. }
  109. }
  110. // this should be an atomic...
  111. volatile int numObjects;
  112. };
  113. static const char* getLeakedObjectClassName() noexcept
  114. {
  115. return OwnerClass::getLeakedObjectClassName();
  116. }
  117. static LeakCounter& getCounter() noexcept
  118. {
  119. static LeakCounter counter;
  120. return counter;
  121. }
  122. };
  123. // -----------------------------------------------------------------------
  124. END_NAMESPACE_DISTRHO
  125. #endif // DISTRHO_LEAK_DETECTOR_HPP_INCLUDED