OpenShot Library | OpenShotAudio  0.2.1
juce_LeakedObjectDetector.h
1 
2 /** @weakgroup juce_core-memory
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  Embedding an instance of this class inside another class can be used as a low-overhead
33  way of detecting leaked instances.
34 
35  This class keeps an internal static count of the number of instances that are
36  active, so that when the app is shutdown and the static destructors are called,
37  it can check whether there are any left-over instances that may have been leaked.
38 
39  To use it, use the JUCE_LEAK_DETECTOR macro as a simple way to put one in your
40  class declaration. Have a look through the juce codebase for examples, it's used
41  in most of the classes.
42 
43  @tags{Core}
44 */
45 template <class OwnerClass>
47 {
48 public:
49  //==============================================================================
50  LeakedObjectDetector() noexcept { ++(getCounter().numObjects); }
51  LeakedObjectDetector (const LeakedObjectDetector&) noexcept { ++(getCounter().numObjects); }
52 
54  {
55  if (--(getCounter().numObjects) < 0)
56  {
57  DBG ("*** Dangling pointer deletion! Class: " << getLeakedObjectClassName());
58 
59  /** If you hit this, then you've managed to delete more instances of this class than you've
60  created.. That indicates that you're deleting some dangling pointers.
61 
62  Note that although this assertion will have been triggered during a destructor, it might
63  not be this particular deletion that's at fault - the incorrect one may have happened
64  at an earlier point in the program, and simply not been detected until now.
65 
66  Most errors like this are caused by using old-fashioned, non-RAII techniques for
67  your object management. Tut, tut. Always, always use std::unique_ptrs, OwnedArrays,
68  ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
69  */
70  jassertfalse;
71  }
72  }
73 
74 private:
75  //==============================================================================
76  class LeakCounter
77  {
78  public:
79  LeakCounter() = default;
80 
81  ~LeakCounter()
82  {
83  if (numObjects.value > 0)
84  {
85  DBG ("*** Leaked objects detected: " << numObjects.value << " instance(s) of class " << getLeakedObjectClassName());
86 
87  /** If you hit this, then you've leaked one or more objects of the type specified by
88  the 'OwnerClass' template parameter - the name should have been printed by the line above.
89 
90  If you're leaking, it's probably because you're using old-fashioned, non-RAII techniques for
91  your object management. Tut, tut. Always, always use std::unique_ptrs, OwnedArrays,
92  ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
93  */
94  jassertfalse;
95  }
96  }
97 
98  Atomic<int> numObjects;
99  };
100 
101  static const char* getLeakedObjectClassName()
102  {
103  return OwnerClass::getLeakedObjectClassName();
104  }
105 
106  static LeakCounter& getCounter() noexcept
107  {
108  static LeakCounter counter;
109  return counter;
110  }
111 };
112 
113 //==============================================================================
114 #if DOXYGEN || ! defined (JUCE_LEAK_DETECTOR)
115  #if (DOXYGEN || JUCE_CHECK_MEMORY_LEAKS)
116  /** This macro lets you embed a leak-detecting object inside a class.
117 
118  To use it, simply declare a JUCE_LEAK_DETECTOR(YourClassName) inside a private section
119  of the class declaration. E.g.
120 
121  @code
122  class MyClass
123  {
124  public:
125  MyClass();
126  void blahBlah();
127 
128  private:
129  JUCE_LEAK_DETECTOR (MyClass)
130  };
131  @endcode
132 
133  @see JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR, LeakedObjectDetector
134  */
135  #define JUCE_LEAK_DETECTOR(OwnerClass) \
136  friend class juce::LeakedObjectDetector<OwnerClass>; \
137  static const char* getLeakedObjectClassName() noexcept { return #OwnerClass; } \
138  juce::LeakedObjectDetector<OwnerClass> JUCE_JOIN_MACRO (leakDetector, __LINE__);
139  #else
140  #define JUCE_LEAK_DETECTOR(OwnerClass)
141  #endif
142 #endif
143 
144 } // namespace juce
145 
146 /** @}*/
Embedding an instance of this class inside another class can be used as a low-overhead way of detecti...