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 6.7KB

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