Loading...
Searching...
No Matches
juce_HeavyweightLeakedObjectDetector.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
template <class OwnerClass>
class HeavyweightLeakedObjectDetector
{
public:
//==============================================================================
HeavyweightLeakedObjectDetector() noexcept { getBacktraceMap()[this] = SystemStats::getStackBacktrace(); }
HeavyweightLeakedObjectDetector (const HeavyweightLeakedObjectDetector&) noexcept { getBacktraceMap()[this] = SystemStats::getStackBacktrace(); }
~HeavyweightLeakedObjectDetector() { getBacktraceMap().erase (this); }
private:
//==============================================================================
typedef std::map<HeavyweightLeakedObjectDetector<OwnerClass>*, String> BacktraceMap;
//==============================================================================
struct BacktraceMapHolder
{
BacktraceMapHolder() = default;
~BacktraceMapHolder()
{
if (map.size() > 0)
{
DBG ("*** Leaked objects detected: " << map.size() << " instance(s) of class " << getLeakedObjectClassName());
DBG (getFormattedBacktracesString());
jassertfalse;
}
}
String getFormattedBacktracesString() const
{
String str;
int counter = 1;
for (auto& bt : map)
{
str << "\nBacktrace " << String (counter++) << "\n"
<< "-----------------------------------------------------------------" << "\n"
<< bt.second;
}
return str;
}
BacktraceMap map;
};
static BacktraceMap& getBacktraceMap()
{
static BacktraceMapHolder holder;
return holder.map;
}
static const char* getLeakedObjectClassName()
{
return OwnerClass::getLeakedObjectClassName();
}
};
//==============================================================================
#if DOXYGEN || ! defined (JUCE_HEAVYWEIGHT_LEAK_DETECTOR)
#if (DOXYGEN || JUCE_CHECK_MEMORY_LEAKS)
#define JUCE_HEAVYWEIGHT_LEAK_DETECTOR(OwnerClass) \
friend class juce::HeavyweightLeakedObjectDetector<OwnerClass>; \
static const char* getLeakedObjectClassName() noexcept { return #OwnerClass; } \
juce::HeavyweightLeakedObjectDetector<OwnerClass> JUCE_JOIN_MACRO (leakDetector, __LINE__);
#else
#define JUCE_HEAVYWEIGHT_LEAK_DETECTOR(OwnerClass)
#endif
#endif
} // namespace juce
juce::String
Definition juce_String.h:53
juce::SystemStats::getStackBacktrace
static String getStackBacktrace()
Definition juce_SystemStats.cpp:177