2013-06-17 09:28:43 -07:00
|
|
|
//------------------------------------------------------------------------------
|
2013-06-17 06:42:49 -07:00
|
|
|
/*
|
2013-06-17 09:28:43 -07:00
|
|
|
This file is part of Beast: https://github.com/vinniefalco/Beast
|
|
|
|
|
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
2013-06-17 06:42:49 -07:00
|
|
|
|
2013-06-17 09:28:43 -07:00
|
|
|
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.
|
2013-06-17 06:42:49 -07:00
|
|
|
|
2013-06-17 09:28:43 -07:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
|
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2013-06-17 06:42:49 -07:00
|
|
|
*/
|
2013-06-17 09:28:43 -07:00
|
|
|
//==============================================================================
|
2013-06-17 06:42:49 -07:00
|
|
|
|
2014-05-29 11:59:36 -07:00
|
|
|
#include <beast/utility/LeakChecked.h>
|
2013-10-04 13:49:24 -07:00
|
|
|
|
|
|
|
|
namespace beast {
|
|
|
|
|
|
2013-09-12 08:26:25 -07:00
|
|
|
namespace detail
|
2013-06-30 09:04:09 -07:00
|
|
|
{
|
2013-06-17 06:42:49 -07:00
|
|
|
|
2013-09-20 16:44:04 -07:00
|
|
|
class LeakCheckedBase::LeakCounterBase::Singleton
|
2013-06-17 06:42:49 -07:00
|
|
|
{
|
|
|
|
|
public:
|
2013-09-20 16:44:04 -07:00
|
|
|
void push_back (LeakCounterBase* counter)
|
2013-06-17 06:42:49 -07:00
|
|
|
{
|
|
|
|
|
m_list.push_front (counter);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-30 09:04:09 -07:00
|
|
|
void checkForLeaks ()
|
2013-06-17 06:42:49 -07:00
|
|
|
{
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
2013-09-20 16:44:04 -07:00
|
|
|
LeakCounterBase* const counter = m_list.pop_front ();
|
2013-06-17 06:42:49 -07:00
|
|
|
|
|
|
|
|
if (!counter)
|
|
|
|
|
break;
|
|
|
|
|
|
2013-06-30 09:04:09 -07:00
|
|
|
counter->checkForLeaks ();
|
2013-06-17 06:42:49 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Singleton& getInstance ()
|
|
|
|
|
{
|
|
|
|
|
static Singleton instance;
|
|
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2013-06-30 09:04:09 -07:00
|
|
|
friend class LeakCheckedBase;
|
|
|
|
|
|
2013-09-20 16:44:04 -07:00
|
|
|
LockFreeStack <LeakCounterBase> m_list;
|
2013-06-17 06:42:49 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2013-09-20 16:44:04 -07:00
|
|
|
LeakCheckedBase::LeakCounterBase::LeakCounterBase ()
|
2014-10-13 14:20:54 -07:00
|
|
|
: m_count (0)
|
2013-06-17 06:42:49 -07:00
|
|
|
{
|
|
|
|
|
Singleton::getInstance ().push_back (this);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-20 16:44:04 -07:00
|
|
|
void LeakCheckedBase::LeakCounterBase::checkForLeaks ()
|
2013-06-17 06:42:49 -07:00
|
|
|
{
|
|
|
|
|
// If there's a runtime error from this line, it means there's
|
|
|
|
|
// an order of destruction problem between different translation units!
|
|
|
|
|
//
|
|
|
|
|
this->checkPureVirtual ();
|
|
|
|
|
|
2014-10-13 14:20:54 -07:00
|
|
|
int const count = m_count.load ();
|
2013-06-17 06:42:49 -07:00
|
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
|
{
|
2014-10-13 14:20:54 -07:00
|
|
|
outputDebugString ("Leaked objects: " + std::to_string (count) +
|
|
|
|
|
" instances of " + getClassName ());
|
2013-06-17 06:42:49 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2013-07-05 07:56:51 -07:00
|
|
|
void LeakCheckedBase::reportDanglingPointer (char const* objectName)
|
|
|
|
|
{
|
2014-10-13 14:20:54 -07:00
|
|
|
outputDebugString (std::string ("Dangling pointer deletion: ") + objectName);
|
2013-07-05 07:56:51 -07:00
|
|
|
bassertfalse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2013-06-30 09:04:09 -07:00
|
|
|
void LeakCheckedBase::checkForLeaks ()
|
2013-06-17 06:42:49 -07:00
|
|
|
{
|
2013-09-20 16:44:04 -07:00
|
|
|
LeakCounterBase::Singleton::getInstance ().checkForLeaks ();
|
2013-06-17 06:42:49 -07:00
|
|
|
}
|
|
|
|
|
|
2013-06-30 09:04:09 -07:00
|
|
|
}
|
2013-10-04 13:49:24 -07:00
|
|
|
|
|
|
|
|
}
|