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

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