OpenShot Audio Library | OpenShotAudio  0.3.1
juce_LeakedObjectDetector.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 //==============================================================================
41 template <class OwnerClass>
43 {
44 public:
45  //==============================================================================
46  LeakedObjectDetector() noexcept { ++(getCounter().numObjects); }
47  LeakedObjectDetector (const LeakedObjectDetector&) noexcept { ++(getCounter().numObjects); }
48 
50  {
51  if (--(getCounter().numObjects) < 0)
52  {
53  DBG ("*** Dangling pointer deletion! Class: " << getLeakedObjectClassName());
54 
66  jassertfalse;
67  }
68  }
69 
70 private:
71  //==============================================================================
72  class LeakCounter
73  {
74  public:
75  LeakCounter() = default;
76 
77  ~LeakCounter()
78  {
79  if (numObjects.value > 0)
80  {
81  DBG ("*** Leaked objects detected: " << numObjects.value << " instance(s) of class " << getLeakedObjectClassName());
82 
90  jassertfalse;
91  }
92  }
93 
94  Atomic<int> numObjects;
95  };
96 
97  static const char* getLeakedObjectClassName()
98  {
99  return OwnerClass::getLeakedObjectClassName();
100  }
101 
102  static LeakCounter& getCounter() noexcept
103  {
104  static LeakCounter counter;
105  return counter;
106  }
107 };
108 
109 //==============================================================================
110 #if DOXYGEN || ! defined (JUCE_LEAK_DETECTOR)
111  #if (DOXYGEN || JUCE_CHECK_MEMORY_LEAKS)
131  #define JUCE_LEAK_DETECTOR(OwnerClass) \
132  friend class juce::LeakedObjectDetector<OwnerClass>; \
133  static const char* getLeakedObjectClassName() noexcept { return #OwnerClass; } \
134  juce::LeakedObjectDetector<OwnerClass> JUCE_JOIN_MACRO (leakDetector, __LINE__);
135  #else
136  #define JUCE_LEAK_DETECTOR(OwnerClass)
137  #endif
138 #endif
139 
140 } // namespace juce