OpenShot Audio Library | OpenShotAudio  0.3.3
juce_HeavyweightLeakedObjectDetector.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 //==============================================================================
42 template <class OwnerClass>
44 {
45 public:
46  //==============================================================================
47  HeavyweightLeakedObjectDetector() noexcept { getBacktraceMap()[this] = SystemStats::getStackBacktrace(); }
49 
50  ~HeavyweightLeakedObjectDetector() { getBacktraceMap().erase (this); }
51 
52 private:
53  //==============================================================================
54  typedef std::map<HeavyweightLeakedObjectDetector<OwnerClass>*, String> BacktraceMap;
55 
56  //==============================================================================
57  struct BacktraceMapHolder
58  {
59  BacktraceMapHolder() = default;
60 
61  ~BacktraceMapHolder()
62  {
63  if (map.size() > 0)
64  {
65  DBG ("*** Leaked objects detected: " << map.size() << " instance(s) of class " << getLeakedObjectClassName());
66  DBG (getFormattedBacktracesString());
67 
76  jassertfalse;
77  }
78  }
79 
80  String getFormattedBacktracesString() const
81  {
82  String str;
83 
84  int counter = 1;
85  for (auto& bt : map)
86  {
87  str << "\nBacktrace " << String (counter++) << "\n"
88  << "-----------------------------------------------------------------" << "\n"
89  << bt.second;
90  }
91 
92  return str;
93  }
94 
95  BacktraceMap map;
96  };
97 
98  static BacktraceMap& getBacktraceMap()
99  {
100  static BacktraceMapHolder holder;
101  return holder.map;
102  }
103 
104  static const char* getLeakedObjectClassName()
105  {
106  return OwnerClass::getLeakedObjectClassName();
107  }
108 };
109 
110 //==============================================================================
111 #if DOXYGEN || ! defined (JUCE_HEAVYWEIGHT_LEAK_DETECTOR)
112  #if (DOXYGEN || JUCE_CHECK_MEMORY_LEAKS)
135  #define JUCE_HEAVYWEIGHT_LEAK_DETECTOR(OwnerClass) \
136  friend class juce::HeavyweightLeakedObjectDetector<OwnerClass>; \
137  static const char* getLeakedObjectClassName() noexcept { return #OwnerClass; } \
138  juce::HeavyweightLeakedObjectDetector<OwnerClass> JUCE_JOIN_MACRO (leakDetector, __LINE__);
139  #else
140  #define JUCE_HEAVYWEIGHT_LEAK_DETECTOR(OwnerClass)
141  #endif
142 #endif
143 
144 } // namespace juce
static String getStackBacktrace()