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