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.

d_leakdetector.hpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 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. /** This macro lets you embed a leak-detecting object inside a class.\n
  30. To use it, simply declare a DISTRHO_LEAK_DETECTOR(YourClassName) inside a private section
  31. of the class declaration. E.g.
  32. \code
  33. class MyClass
  34. {
  35. public:
  36. MyClass();
  37. void blahBlah();
  38. private:
  39. DISTRHO_LEAK_DETECTOR(MyClass)
  40. };
  41. \endcode
  42. */
  43. #define DISTRHO_LEAK_DETECTOR(ClassName) \
  44. friend class DISTRHO_NAMESPACE::LeakedObjectDetector<ClassName>; \
  45. static const char* getLeakedObjectClassName() noexcept { return #ClassName; } \
  46. DISTRHO_NAMESPACE::LeakedObjectDetector<ClassName> DISTRHO_JOIN_MACRO(leakDetector_, ClassName);
  47. #define DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ClassName) \
  48. DISTRHO_DECLARE_NON_COPY_CLASS(ClassName) \
  49. DISTRHO_LEAK_DETECTOR(ClassName)
  50. //==============================================================================
  51. /**
  52. Embedding an instance of this class inside another class can be used as a low-overhead
  53. way of detecting leaked instances.
  54. This class keeps an internal static count of the number of instances that are
  55. active, so that when the app is shutdown and the static destructors are called,
  56. it can check whether there are any left-over instances that may have been leaked.
  57. To use it, use the DISTRHO_LEAK_DETECTOR macro as a simple way to put one in your
  58. class declaration.
  59. */
  60. template<class OwnerClass>
  61. class LeakedObjectDetector
  62. {
  63. public:
  64. //==============================================================================
  65. LeakedObjectDetector() noexcept { ++(getCounter().numObjects); }
  66. LeakedObjectDetector(const LeakedObjectDetector&) noexcept { ++(getCounter().numObjects); }
  67. ~LeakedObjectDetector() noexcept
  68. {
  69. if (--(getCounter().numObjects) < 0)
  70. {
  71. /** If you hit this, then you've managed to delete more instances of this class than you've
  72. created.. That indicates that you're deleting some dangling pointers.
  73. Note that although this assertion will have been triggered during a destructor, it might
  74. not be this particular deletion that's at fault - the incorrect one may have happened
  75. at an earlier point in the program, and simply not been detected until now.
  76. Most errors like this are caused by using old-fashioned, non-RAII techniques for
  77. your object management. Tut, tut. Always, always use ScopedPointers, OwnedArrays,
  78. ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
  79. */
  80. d_stderr2("*** Dangling pointer deletion! Class: '%s', Count: %i", getLeakedObjectClassName(), getCounter().numObjects);
  81. }
  82. }
  83. private:
  84. //==============================================================================
  85. class LeakCounter
  86. {
  87. public:
  88. LeakCounter() noexcept
  89. : numObjects(0) {}
  90. ~LeakCounter() noexcept
  91. {
  92. if (numObjects > 0)
  93. {
  94. /** If you hit this, then you've leaked one or more objects of the type specified by
  95. the 'OwnerClass' template parameter - the name should have been printed by the line above.
  96. If you're leaking, it's probably because you're using old-fashioned, non-RAII techniques for
  97. your object management. Tut, tut. Always, always use ScopedPointers, OwnedArrays,
  98. ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
  99. */
  100. d_stderr2("*** Leaked objects detected: %i instance(s) of class '%s'", numObjects, getLeakedObjectClassName());
  101. }
  102. }
  103. // this should be an atomic...
  104. volatile int numObjects;
  105. };
  106. static const char* getLeakedObjectClassName() noexcept
  107. {
  108. return OwnerClass::getLeakedObjectClassName();
  109. }
  110. static LeakCounter& getCounter() noexcept
  111. {
  112. static LeakCounter counter;
  113. return counter;
  114. }
  115. };
  116. // -----------------------------------------------------------------------
  117. END_NAMESPACE_DISTRHO
  118. #endif // DISTRHO_LEAK_DETECTOR_HPP_INCLUDED