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