OpenShot Audio Library | OpenShotAudio  0.6.0
juce_LeakedObjectDetector.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
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 
49  LeakedObjectDetector& operator= (const LeakedObjectDetector&) noexcept = default;
50 
52  {
53  if (--(getCounter().numObjects) < 0)
54  {
55  DBG ("*** Dangling pointer deletion! Class: " << getLeakedObjectClassName());
56 
68  jassertfalse;
69  }
70  }
71 
72 private:
73  //==============================================================================
74  class LeakCounter
75  {
76  public:
77  LeakCounter() = default;
78 
79  ~LeakCounter()
80  {
81  if (numObjects.value > 0)
82  {
83  DBG ("*** Leaked objects detected: " << numObjects.value << " instance(s) of class " << getLeakedObjectClassName());
84 
92  jassertfalse;
93  }
94  }
95 
96  Atomic<int> numObjects;
97  };
98 
99  static const char* getLeakedObjectClassName()
100  {
101  return OwnerClass::getLeakedObjectClassName();
102  }
103 
104  static LeakCounter& getCounter() noexcept
105  {
106  static LeakCounter counter;
107  return counter;
108  }
109 };
110 
111 //==============================================================================
112 #if DOXYGEN || ! defined (JUCE_LEAK_DETECTOR)
113  #if (DOXYGEN || JUCE_CHECK_MEMORY_LEAKS)
133  #define JUCE_LEAK_DETECTOR(OwnerClass) \
134  friend class juce::LeakedObjectDetector<OwnerClass>; \
135  static const char* getLeakedObjectClassName() noexcept { return #OwnerClass; } \
136  juce::LeakedObjectDetector<OwnerClass> JUCE_JOIN_MACRO (leakDetector, __LINE__);
137  #else
138  #define JUCE_LEAK_DETECTOR(OwnerClass)
139  #endif
140 #endif
141 
142 } // namespace juce