diff --git a/ememory/RefCounter.cpp b/ememory/RefCounter.cpp index 8f1d4d9..8ef9c98 100644 --- a/ememory/RefCounter.cpp +++ b/ememory/RefCounter.cpp @@ -10,6 +10,22 @@ ETK_DECLARE_TYPE(ememory::RefCounter); +#ifdef DEBUG + static int32_t& getLocalDebugCounter() { + static int32_t g_uid = 0; + return g_uid; + } + void ememory::resetDebugRefCounter() { + getLocalDebugCounter() = 0; + } +#endif + +ememory::RefCounter::RefCounter() { + #ifdef DEBUG + m_uid = getLocalDebugCounter()++; + #endif +} + ememory::RefCounter::~RefCounter() { if (m_refCount != 0) { EMEMORY_ERROR("delete a RefCounted element that is keep by somewhere !! " << m_refCount); @@ -30,11 +46,23 @@ void ememory::RefCounter::refRelease() { return; } if (refCount < 0) { - EMEMORY_ERROR("request release a refcounted One more time than needed !! " << m_refCount); + #ifdef DEBUG + EMEMORY_ERROR("request release a refcounted One more time than needed !! " << m_refCount << " uid=" << m_uid); + #else + EMEMORY_ERROR("request release a refcounted One more time than needed !! " << m_refCount); + #endif m_refCount = 0; } } int ememory::RefCounter::getRefCount() const { return m_refCount; -} \ No newline at end of file +} + + +uint64_t ememory::RefCounter::getRawPointer() const { + #ifdef DEBUG + return m_uid; + #endif + return uint64_t(this); +} diff --git a/ememory/RefCounter.hpp b/ememory/RefCounter.hpp index 593d43c..7077d5f 100644 --- a/ememory/RefCounter.hpp +++ b/ememory/RefCounter.hpp @@ -19,10 +19,14 @@ namespace ememory { #else public: #endif + RefCounter(); // Virtualize destructor in private to prevent user ot remove it without permition virtual ~RefCounter(); private: int32_t m_refCount = 1; + #ifdef DEBUG + int32_t m_uid; + #endif public: /** * @brief Keep a copy of this reference-counted element. @@ -46,7 +50,15 @@ namespace ememory { * @return Request const SharedPtr */ //const ememory::RefPtr refFromThis() const; + protected: + /** + * @brief get the RAW pointer value of this element (for debug only) + */ + uint64_t getRawPointer() const; }; + #ifdef DEBUG + void resetDebugRefCounter(); + #endif } #include