[DEV] add a unique id in debug mode ==> simplify debug

This commit is contained in:
Edouard DUPIN 2019-08-18 12:37:20 +02:00
parent d3fa101aeb
commit 618e2b7a2a
2 changed files with 42 additions and 2 deletions

View File

@ -10,6 +10,22 @@
ETK_DECLARE_TYPE(ememory::RefCounter); 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() { ememory::RefCounter::~RefCounter() {
if (m_refCount != 0) { if (m_refCount != 0) {
EMEMORY_ERROR("delete a RefCounted element that is keep by somewhere !! " << m_refCount); EMEMORY_ERROR("delete a RefCounted element that is keep by somewhere !! " << m_refCount);
@ -30,11 +46,23 @@ void ememory::RefCounter::refRelease() {
return; return;
} }
if (refCount < 0) { 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; m_refCount = 0;
} }
} }
int ememory::RefCounter::getRefCount() const { int ememory::RefCounter::getRefCount() const {
return m_refCount; return m_refCount;
} }
uint64_t ememory::RefCounter::getRawPointer() const {
#ifdef DEBUG
return m_uid;
#endif
return uint64_t(this);
}

View File

@ -19,10 +19,14 @@ namespace ememory {
#else #else
public: public:
#endif #endif
RefCounter();
// Virtualize destructor in private to prevent user ot remove it without permition // Virtualize destructor in private to prevent user ot remove it without permition
virtual ~RefCounter(); virtual ~RefCounter();
private: private:
int32_t m_refCount = 1; int32_t m_refCount = 1;
#ifdef DEBUG
int32_t m_uid;
#endif
public: public:
/** /**
* @brief Keep a copy of this reference-counted element. * @brief Keep a copy of this reference-counted element.
@ -46,7 +50,15 @@ namespace ememory {
* @return Request const SharedPtr * @return Request const SharedPtr
*/ */
//const ememory::RefPtr<EMEMORY_TYPE> refFromThis() const; //const ememory::RefPtr<EMEMORY_TYPE> 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 <ememory/details/EnableSharedFromThis.hxx> #include <ememory/details/EnableSharedFromThis.hxx>